猿问

如何获得Python导航到链接并从此子链接打印多个数据点?

我正在查看的是父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 office

January 3, 1987

Assumed office

April 10, 2018

我怎样才能做到这一点?


对于导航到几个不同的链接,我认为它将看起来像这样...


from urllib.parse import urljoin

senator_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'


白板的微信
浏览 150回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答