Python BeautifulSoup无法识别div标签

时间:2020-01-06 09:30:35

标签: python html web-scraping beautifulsoup

在这里,我正在使用Python bs4进行网络抓取。我想过滤掉div属性值为class的{​​{1}}标记。这个div标签确实存在(如图所示),但是BeautifulSoup无法识别div标签。想知道为什么吗?

这是屏幕截图。 link

编辑: 附加代码:

a-column a-span6 a-span-last

1 个答案:

答案 0 :(得分:0)

好的。我看到了。

问题出在html.parser上。根据使用的解析器,您可以获得不同的结果。进一步了解here

但是更改为'lxml'给了我输出:

from bs4 import BeautifulSoup
import urllib.request
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url='https://www.amazon.com/Acer-SB220Q-Ultra-Thin-Frame-Monitor/dp/B07CVL2D2S/ref=zg_bs_electronics_35?_encoding=UTF8&psc=1&refRID=YGK101A649HEC8NXXM1T'
req=urllib.request.Request(url=url, headers={'User-Agent':'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'})
response=urllib.request.urlopen(url=req,context=ctx)
html=response.read().decode('utf-8')
soup=BeautifulSoup(html,'lxml')   # <----- CHANGE MADE HERE
soup.find_all('div',{'class':'a-column a-span6 a-span-last'})
相关问题