BS4找不到元素
问题描述:
import urllib2
from bs4 import BeautifulSoup
url = 'https://www.wsj.com/news/world'
page = urllib2.urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
l = soup.find_all('div', {'class': 'module module_ls ls ni noImage'})
a = soup.find_all('a', {'class': 'subPrev headline'})
print(l)
print(a)
这会返回空列表。但正如你可以在图像中看到的那样,这些类存在div ......为什么?BS4找不到元素
答
随着lxml
解析器它的工作原理:
soup = BeautifulSoup(page, 'lxml')
当然你必须安装LXML。如果您有html5lib
它也适用于页面:
soup = BeautifulSoup(page, 'html5lib')
谢谢,但它也不工作。对你起作用吗? – ben
没有。这很奇怪。 – phd
哈哈,是啊,这真是太痛苦了 – ben