我只想从源代码(div代码为“ col-green”的html代码)中提取文本。当我只想提取源代码中的文本时会出现警告。
from bs4 import BeautifulSoup
import requests
page_link = 'http://drneclayazicioglu.meb.k12.tr/'
page_response = requests.get(page_link, timeout=5)
page_content = BeautifulSoup(page_response.content, "html.parser")
source_code=(page_content.findAll('div',attrs={"id":"col-green"}))
soup = BeautifulSoup(source_code) #error line here...
错误是:
Warning (from warnings module):
File "C:/Users/Emre/Desktop/python.py", line 7
soup = BeautifulSoup(source_code)
UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("html.parser"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
The code that caused this warning is on line 7 of the file C:/Users/Emre/Desktop/python.py. To get rid of this warning, pass the additional argument 'features="html.parser"' to the BeautifulSoup constructor.
最佳答案
您无需再次使用BeautifulSoup
。您的source_code
返回bs4.element.ResultSet
,您可以这样获得文本:
for a in source_code:
print a.text
输出:
Duyurular
Ocak 2019 GELİR LİSTEMİZ 11.02.2019 00:18GİDER LİSTEMİZ OCAK AYI11.02.2019
00:11Yönetici Görevlendirme Yönetmeliğinde Değişiklik10.02.2019 23:512018-2019
ÖGRETIM YILI ÖĞRETMENLER KURULU TOPLANTISI YAPILDI05.02.2019 18:49BİN DÖRT YÜZ
ÖĞRENCİ 5 YETİME KARDEŞ OLDU!!!06.01.2019 11:18
Devamı...
关于python - Beautiful Soup UserWarning:未明确指定解析器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54754754/