问题刮多个网址NFL投注d

2024-06-25 23:55:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两段代码: 第一:在博彩网站上摘录当前的NFL比赛 第二:提取游戏URL上的所有赌注

第一个问题是我不知道如何将它们构建到1个代码中。现在我将结果导出到Excel,并使用VBA添加URL的开头以及正确的“”和逗号。我玩了地图和加入,但无法使它工作

然而,Bigges的问题是,我设置的多个URL scrape有缺陷——因为我只从第一个scrape获取数据

URL抓取:

import requests
from bs4 import BeautifulSoup
result = requests.get("https://www.betfair.com/sport/american-football")
src = result.content
soup = BeautifulSoup(src, 'lxml')

links = [a['href'] for a in soup.find_all('a',{'data-competition': "NFL Preseason Matches"},href=True)]



print(list(set(links)))
#df.to_csv('file.csv')

#str_concat = ',https://www.betfair.com'.join(list(links))

#print(list(set(links)))
#def myfunc(a, b):
#           return a + b

#x = map(myfunc, ('https://www.betfair.com','https://www.betfair.com'), (links))

多URL刮取:

import requests
from bs4 import BeautifulSoup
import pandas as pd
urls = ['https://www.betfair.com/sport/american-football/nfl-preseason-matches/minnesota-vikings-buffalo-bills/29427759',
        'https://www.betfair.com/sport/american-football/nfl-preseason-matches/los-angeles-rams-houston-texans/29427770',
        'https://www.betfair.com/sport/american-football/nfl-preseason-matches/pittsburgh-steelers-carolina-panthers/29427758']

for url in urls:
    result2 = requests.get(url)
    src2 = result2.content
    soup = BeautifulSoup(src2, 'lxml')

data = []
for item in soup.find_all('div', {'class': 'minimarketview-content'}):
    temp_data = [ alpha for alpha in item.text.split('\n') if alpha != '' ] 
    data.append(temp_data)

df = pd.DataFrame(data)
print(df)

df.to_csv('file2.csv')

我希望在一个文件中显示所有3个URL的结果,但只显示最后一个URL的结果:

0,1,2,3,4,5,6,7,8
0,Pittsburgh Steelers,1.42,Carolina Panthers,2.6,,,,,
1,Pittsburgh Steelers,1.75,"-3,5",Carolina Panthers,1.95,"+3,5",,,
2,Nuværende antal points:,Over,1.8,"+33,5",Under,1.9,"+33,5",,
3,Pittsburgh Steelers,1.83,-4,Uafgjort,20.0,+4,Carolina Panthers,1.9,+4
4,Pittsburgh Steelers (-4.5) & Over (33.5) points,3.4,Pittsburgh Steelers (-4.5) og under (33.5) point,3.75,Carolina Panthers (+4.5) & Over (33.5) points,3.5,Carolina Panthers (+4.5) og under (33.5) point,3.5,

Tags: httpsimportcomurldatawwwlinksrequests
1条回答
网友
1楼 · 发布于 2024-06-25 23:55:37

您只得到最后一页,因为您正在覆盖soup,而不首先进行处理并将其发送到数据

使用一个处理来自for循环的数据的函数,这样做可能会更好

不过,这段代码还可以写得更好

def process_data(soup: BeautifulSoup):
    for item in soup.find_all('div', {'class': 'minimarketview-content'}):
        temp_data = [alpha for alpha in item.text.split('\n') if alpha != '']
        data.append(temp_data)


for url in urls:
    result2 = requests.get(url)
    src2 = result2.content
    soup = BeautifulSoup(src2, 'lxml')
    process_data(soup)


df = pd.DataFrame(data)
print(df)
df.to_csv('file2.csv')

更新:

要动态地获取您正在寻找的链接,您必须首先请求https://www.betfair.com/,然后在那里查找

<div class="nav open" data-nav="All Sports" style="display: block;"/>

这个div包含了所提供运动的所有类别的列表。在你要找的名单上循环

<span>American Football</span>

然后你得到下一个链接,然后重复这个过程寻找你感兴趣的块,直到你到达链的末端

要分析您感兴趣的块,请使用web浏览器中的“Inspect”,右键单击您感兴趣的块,它将打开Dev Tools,您将确切地知道您要分析的块

祝你好运

相关问题 更多 >