我正在查看的是父URL。https://en.wikipedia.org/wiki/List_of_current_members_of_the_United_States_Senate从那里,我想让Python单击几个链接,所有链接都是('td')[3] .a ['href']。父 URL 中的前三个是: 'Richard Shelby', 'Doug Jones', and 'Lisa Murkowski'。所有子链接都有与此匹配的文本: 'Assumed office'。我想抓住所有这些日期'Assumed office'。因此,因为'Richard Shelby'它将是:Assumed officeJanuary 3, 1987Assumed officeApril 10, 2018我怎样才能做到这一点?对于导航到几个不同的链接,我认为它将看起来像这样...from urllib.parse import urljoinsenator_link = "https://en.wikipedia.org/wiki/List_of_current_members_of_the_United_States_Senate"senator_link = row.find_all('td')[3].a['href']senator_link = urljoin(link, senator_link)response = session.get(senator_link)with requests.Session() as session: html = session.get(link).text soup = BeautifulSoup(response.content, "lxml") res = soup.findAll("span", {"class": "nowrap"}) for r in res: print("Assumed Office: " + r.find("span", {'class': 'nowrap'}).text)我得到的那段代码是这样的:AttributeError: 'NoneType' object has no attribute 'text'
添加回答
举报
0/150
提交
取消