我正在查看的是父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我怎樣才能做到這一點?對于導(dǎo)航到幾個不同的鏈接,我認(rèn)為它將看起來像這樣...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
提交
取消