【问题标题】:How can I get URLs from Oddsportal?如何从 Oddsportal 获取 URL?
【发布时间】:2021-07-01 07:09:08
【问题描述】:

如何从该特定链接获取所有 URL:https://www.oddsportal.com/results/#soccer

对于此页面上的每个 URL,都有多个页面,例如页面第一个链接:

https://www.oddsportal.com/soccer/africa/

以以下页面为例:

https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/

->https://www.oddsportal.com/soccer/africa/africa-cup-of-nations/results/#/page/2/...

https://www.oddsportal.com/soccer/africa/africa-cup-of-nations-2019/results/

->https://www.oddsportal.com/soccer/africa/africa-cup-of-nations-2019/results/#/page/2/...

理想情况下,我想用 python 编写代码,因为我对它很熟悉(比其他语言更不接近我可以称之为舒适的语言)

点击链接后:

当我去检查元素时,我可以看到链接可以被刮掉,但是我对它很陌生。

请帮忙

【问题讨论】:

  • 请发布您的代码。你尝试了什么,你在哪里卡住了?
  • 现在,我正在手动复制页面上的每个 URL 并创建一个列表。因此,我不确定我可以在这里发布什么代码
  • 你是网络抓取的新手吗?
  • @Ram 是的。很新。我创建了一个代码,可以“在 URL 中”抓取所有数据。但是,要获得我需要帮助的所有 URL。由于 For 循环和所有属性,我无法理解它。
  • @Ram 如果我可以在此处发布现有代码以从 URL 中抓取数据,会有帮助吗?

标签: python web-scraping


【解决方案1】:

我已经从您提到的主页中提取了 URL。

import requests
import bs4 as bs

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'])

由于您是网络抓取的新手,我建议您阅读这些内容。

  • Beautiful Soup - Beautiful Soup 是一个 Python 库,用于从 HTML 和 XML 文件中提取数据。

文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/

  • requests - Requests 是一个优雅而简单的 Python HTTP 库。

文档:https://docs.python-requests.org/en/master/

  • Selenium - Selenium 是一个涵盖一系列工具和库的总括项目,这些工具和库支持和支持网络浏览器的自动化。

文档:https://selenium-python.readthedocs.io/

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
相关资源
最近更新 更多