使用 BeautifulSoup 提取 <a> 标签

Extracting <a> tags using BeautifulSoup

我在从 https://www.symantec.com/index.jsp 中提取 'a' 标签时遇到问题。

以下是代码,它给了我 'links'.

的空集
from bs4 import BeautifulSoup
import urllib2

response = urllib2.urlopen('https://www.symantec.com/index.jsp').read()
soup = BeautifulSoup(response, 'html.parser')
links = soup.find_all('a')
print(links)

对于其他网址,代码可以正常工作,但不适用于此网址。是因为 index.jsp 是动态的吗?可能的解决方案是什么?

我正在使用 python 2.7.

将解析器更改为 html5liblxml:

soup = BeautifulSoup(response, 'html5lib')
soup = BeautifulSoup(response, 'lxml')

需要html5liblxmlto be installed:

pip install html5lib
pip install lxml

证明:

>>> from bs4 import BeautifulSoup
>>> import urllib2
>>> 
>>> response = urllib2.urlopen('https://www.symantec.com/index.jsp').read()
>>> len(BeautifulSoup(response, 'html.parser').find_all("a"))
0
>>> len(BeautifulSoup(response, 'html5lib').find_all("a"))
187
>>> len(BeautifulSoup(response, 'lxml').find_all("a"))
187

另请参阅文档的相关部分: