BS4找不到元素

时间:2017-07-16 16:06:20

标签: python beautifulsoup urllib

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 ...为什么?

element inspection of website

1 个答案:

答案 0 :(得分:0)

使用lxml解析器,它可以工作:

soup = BeautifulSoup(page, 'lxml')

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

soup = BeautifulSoup(page, 'html5lib')