Skip to main content
 首页 » 编程设计

python之无法使用 python-BeautifulSoup 获取标签下的所有节点

2025年04月02日18cmt

我正在抓取的页面是 link .我想获得所有的 a href 论文。后续代码如下:

import urllib2 
import requests 
from bs4 import BeautifulSoup 
 
response = requests.get('http://ijcai.org/proceedings/2011') 
soup = BeautifulSoup(response.content, 'html.parser') 
page = soup.find('div', class_ ='field-item even') 
tree = [child for child in page.children] 

但是当我尝试:tree[-1] 时,我得到了:

<a href="Erratum049.html">Erratum</a> 

确实它只是放在了页面的一半。我什么时候未能获得该页面的其余部分?你有什么想法吗?提前致谢!

请您参考如下方法:

此页面的 HTML 格式不正确,请使用 different parser ,例如html5lib(需要安装 html5lib):

soup = BeautifulSoup(response.content, 'html5lib') 

或lxml(需要安装lxml):

soup = BeautifulSoup(response.content, 'lxml') 

现在 tree[-1] 将是页面的最后一段:

<p><a href="http://ijcai.org/Proceedings/11/Papers/507.pdf">Index</a> / 2871</p> 

我还会改进您提取链接的方式:

links = [a["href"] for a in soup.select(".field-item a")]