美丽的汤用户警告:未明确指定解析器

时间:2019-02-18 20:18:34

标签: python python-3.x beautifulsoup

我只想从源代码(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.

2 个答案:

答案 0 :(得分:1)

您无需再次使用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ı...

答案 1 :(得分:0)

<f:debug>_all</f:debug>

我希望这行得通!