【发布时间】:2018-09-02 20:31:28
【问题描述】:
根据标题,我已经抓取了我感兴趣的网页并将 URL 保存在一个变量中。
import requests
from bs4 import BeautifulSoup
for pagenumber in range(1, 2):
url = 'https://www.congress.gov/search?q=%7B%22source%22%3A%22legislation%22%2C%22congress%22%3A%22112%22%7D&page={}'.format(pagenumber)
res = requests.get(url, headers = {'User-agent': 'Chrome'})
soup = BeautifulSoup(res.text, 'html.parser')
lists = soup.find_all("li", {"class" : "expanded"})
for bill in lists:
block = bill.find("span", {"class":"result-item"})
link_cosponsors = block.find_all("a")[1]['href'] # I am interested in the second URL
最后一行是给我的 URL 列表。现在我正在努力访问每个 URL 并从每个 URL 中抓取新信息。
for url in link_cosponsors:
soup_cosponsor = BeautifulSoup(requests.get(url).text, 'html.parser')
table = soup.find('table', {'class':'item_table'})
我认为问题在于创建 link_cosponsors 的方式,即列表的第一个元素不是完整的“https://etc”。但只有“h”,因为我收到错误“无效的 URL 'h':未提供架构。也许你的意思是 http://h?”。 我已尝试将链接附加到列表中,但这也不起作用。
【问题讨论】:
标签: python url beautifulsoup