【发布时间】:2021-07-01 08:47:58
【问题描述】:
我有一个代码可以从oddsportal.com 主页上抓取所有网址。 我想要父 URL 中所有页面的后续链接 例如 https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/ 还有更多页面,即https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/、https://www.oddsportal.com/soccer/africa/africa-cup-of-nations-2019/results/ 等。 我怎样才能得到它?
我现有的代码:
import requests
import bs4 as bs
import pandas as pd
url = 'https://www.oddsportal.com/results/#soccer'
headers = {
'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
resp = requests.get(url, headers=headers)
soup = bs.BeautifulSoup(resp.text, 'html.parser')
base_url = 'https://www.oddsportal.com'
a = soup.findAll('a', attrs={'foo': 'f'})
# This set will have all the URLs of the main page
s = set()
for i in a:
s.add(base_url + i['href'])
s = list(s)
# This will filter for all soccer URLs
s = [x for x in s if '/soccer/' in x]
s = pd.DataFrame(s)
print(s)
我对网络抓取非常陌生,因此提出了这个问题。
【问题讨论】:
-
@Qharr 我该怎么做?
-
为什么不像在主页上那样直接进入你在
s中获得的网址?
标签: python web-scraping beautifulsoup