使用URL中的变量循环抓取网站中多个页面的数据

用户名

我想基于变量{event}从request2中的URL循环抓取多个页面。这段代码基本上将轮流浏览数据集“ eventid”,并根据request2中的变量{event}抓取URL的多个页面。我遇到的问题是代码仅从'eventid'(991215)刮取最后一个事件的页面,然后停止。

>>> request1 = requests.get('https://www.odds.com.au/api/web/public/Meetings/getDataByRangeCacheable/?filter=events,regions,meetings&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&sportId=1&regionId[]=1&regionId[]=22&regionId[]=24&regionId[]=25&regionId[]=26&regionId[]=27&regionId[]=28&regionId[]=29&regionId[]=30&rangeStart=2020-02-19T16:00:00.356Z&rangeEnd=2020-02-20T15:59:59.356Z ')

# Data set from request1

>>> eventid = []
>>> json1 = request1.json()
>>> for id in json1.get('events'):
        ...     eventid.append(id['id'])

>>> print(eventid)
[990607, 990111, 990594, 990614, 990608, 990112, 990595, 990615, 990609, 990113, 990114, 990115, 990116, 990117, 990118, 990119, 990324, 990325, 990326, 990327, 990295, 990286, 990328, 990318, 990296, 990287, 990329, 990319, 990297, 990288, 990330, 990320, 990311, 990298, 990289, 990331, 990321, 990312, 990299, 990290, 990322, 990313, 990300, 990291, 989959, 990323, 990314, 990301, 990292, 989960, 990315, 989822, 989961, 990316, 990303, 990293, 989962, 990317, 990304, 990294, 989963, 990305, 989964, 990306, 989965, 990307, 989966, 990308, 990309, 990310, 991142, 991143, 991144, 991145, 991146, 991232, 991211, 991218, 991147, 991233, 990583, 991212, 991219, 991148, 991234, 990584, 991213, 991220, 991235, 991149, 990585, 991214, 991221, 991236, 990586, 991215]

# Code I am having trouble with

>>> for event in eventid:
         ...     request2 = requests.get(f'https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId={event}&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'})
乔治

该代码是确定的,并且确实提取了for event in eventid:循环中的每个事件请尝试以下操作:

for event in eventid:    
    request2 = requests.get(f'https://www.punters.com.au/api/web/public/Odds/getOddsComparisonCacheable/?allowGet=true&APIKey=65d5a3e79fcd603b3845f0dc7c2437f0&eventId={event}&betType=FixedWin', headers={'User-Agent': 'Mozilla/5.0'})
    tmp_json = request2.json()
    print(tmp_json['eventId'], tmp_json['eventNameFull'])

输出将是:

990607 Ludlow Race 5 - 5234m
990111 Newcastle Race 5 - 1765m
990594 Punchestown Race 6 - 4400m
990614 Doncaster Race 5 - 5721m
990608 Ludlow Race 6 - 3512m
...

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用python和beautifulsoup4循环抓取网站中多个页面的数据

从具有多个页面的网站抓取数据

使用R中的JavaScript抓取页面的数据链接

如何使用scrapy抓取具有多个页面的网站

从网站的所有页面中抓取数据

无法使用 selenium python 从网站中抓取多个 URL

如何列出使用 Scrapy 抓取数据的页面的 URL?

使用VBA从网站中抓取数据

抓取抓取多个页面,提取数据并保存到mysql中

如何使用BeautifulSoup创建循环以从源URL抓取多个页面?

使用 BeautifulSoup 在 python 中抓取多个页面

使用 BeautifulSoup 在 Steam 中抓取多个页面

如何使用循环从 API 中抓取数据

如何从inspect元素而不是页面的源代码中抓取网站的href(随会话变化)

如何将具有多个页面和内部链接的网站抓取到 Pandas 数据框中?

从网站列表中抓取数据

我正在尝试使用scrapy抓取网站并将已抓取的数据存储到item类的变量中

使用bs4在同一页面的多个表中抓取特定的html表

使用rvest抓取带有登录页面的网站

从 URL 中带有“#”的网站抓取数据时出错

使用“分页”更改网站中页面的宏

使用 Python 将多个 URL 中的不同变量抓取到一个 CSV 文件中

在抓取蜘蛛中解析URL之前,如何抓取表示网站中最大页面数的数字?

使用scrapy-selenium模块从多个JavaScript页面中抓取硒数据

如何使用python从具有多个动态选择字段的页面中抓取数据?

BeautifulSoup Web抓取多个页面的URL不变

网络抓取多个页面的最终数据帧

在子iframe中获取父页面的隐藏变量数据

如何使用循环抓取页面的所有项目