2017-07-16 89 views
0
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找不到元素

element inspection of website

回答

0

随着lxml解析器它的工作原理:

soup = BeautifulSoup(page, 'lxml') 

当然你必须安装LXML。如果您有html5lib它也适用于页面:

soup = BeautifulSoup(page, 'html5lib') 
+0

谢谢,但它也不工作。对你起作用吗? – ben

+0

没有。这很奇怪。 – phd

+0

哈哈,是啊,这真是太痛苦了 – ben