BeautifulSoup不让我得到文本

时间:2018-11-11 18:10:21

标签: python beautifulsoup

我正在寻找标签中所有的文本

它为我提供了控制台中的文本,但没有将其放在.txt文件中。

它适用于body.text,但不适用于article.text。我不知道该怎么办。

import bs4 as bs
import urllib.request
#import re

sauce = urllib.request.urlopen('http://www.bodoniparavia.it/index.php/it/amministrazione-trasparente/bandi-di-gara-e-contratti.html')
soup = bs.BeautifulSoup(sauce,'lxml')


body = soup.body
article = body.find('article')
article1 = article.text
print(article1)

x = open('file.txt','w')
x.write(article1)
x.close

1 个答案:

答案 0 :(得分:1)

这对我来说似乎很好,但是尝试将Sub SortColl(ByRef c As Collection) ' Purpose: sort collection by keys via bubble sort method Dim i As Long, j As Long Dim vTemp As Variant For i = 1 To c.Count - 1 For j = i + 1 To c.Count If c(i) > c(j) Then ' remember the lesser item vTemp = c(j) ' remove the lesser item c.Remove j ' add the lesser item before the greater one c.Add vTemp, vTemp, i End If Next j Next i End Sub 添加到write语句中。所以代码现在看起来像这样

encoding = 'utf-8'