我试图抓住大学的律师参加了一个特定的律师事务所,但我不知道如何抓住这两所大学列出的链接:https://www.wlrk.com/attorney/hahn/。如第一幅图片所示,这位律师就读的两所大学分别被贴上了“li”的标签
当我运行下面的代码时,我只得到第一个'li'标记末尾的html(如第二个链接图像中所示),而不是第二个li部分,因此我只得到第一所大学“Carleton College:”
import requests
from bs4 import BeautifulSoup as soup
url = 'https://www.wlrk.com/attorney/hahn/'
res = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0'})
personal_soup = soup(res.content, "html.parser")
education = personal_soup.find("div",{'class':'attorney--education'})
education.li.a.text # 'Carleton University'
bs只获取第一个li元素。我不知道为什么。如果你想尝试使用lxml,这里有一个方法
输出:
更改您的解析器,我将使用
select
并直接指向a
元素lxml'更宽容,可以处理不应该出现的错误的结束a
标记。而且,find
只会返回第一个匹配,而find_all
则返回所有匹配例如
<a href="/attorneys/?asf_ugs=257">Carleton College</a></a>
source
相关问题 更多 >
编程相关推荐