【问题标题】:Issue scraping multiple URLs - NFL betting data抓取多个 URL 的问题 - NFL 投注数据
【发布时间】:2019-08-29 10:49:29
【问题描述】:

我有 2 段代码: 1st:提取投注网站上当前的 NFL 比赛 第二:提取游戏网址上的所有投注

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

然而,最大的问题是我设置的多个 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))

多网址抓取:

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 的结果都在一个文件中,但只显示最后一个的结果:

,

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,

【问题讨论】:

    标签: python pandas beautifulsoup


    【解决方案1】:

    您只得到最后一页被刮掉,因为您在没有先处理并将其发送到数据的情况下覆盖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>

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

    要分析您对哪些块感兴趣,请使用网络浏览器中的“检查”,右键单击您感兴趣的块,它将打开开发工具,您将确切知道您要分析的块。

    祝你好运

    【讨论】:

    • 感谢您的评论。有用!关于如何实现将 URL 从第一个代码动态获取到代码中的任何指针?或者使用什么功能。
    • 你应该为那个问题提出一个不同的问题。你不应该发布双重问题的帖子。如果这解决了您的第二个问题,也许您可​​以考虑将其标记为已解决。对于您的第一个问题,最好再提出一个问题,我会用提示更新我的答案。
    • 好的,很抱歉 :)。
    • @mvp1988 无需抱歉,不是警告,只是有时看起来很苛刻,把它当作一个友好的建议;-)
    猜你喜欢
    • 1970-01-01
    • 2020-06-28
    • 2021-03-15
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多