Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
web scraper中的Python键错误_Python_Web Scraping - Fatal编程技术网

web scraper中的Python键错误

web scraper中的Python键错误,python,web-scraping,Python,Web Scraping,我在我的网页抓取程序中遇到一个关键错误:“title”错误,不确定问题出在哪里。当我在网页上使用inspect元素时,我可以看到我试图查找的元素 import pandas as pd import requests from bs4 import BeautifulSoup import re url = 'https://www.ncaagamesim.com/college-basketball-predictions.asp' response = requests.get(url)

我在我的网页抓取程序中遇到一个关键错误:“title”错误,不确定问题出在哪里。当我在网页上使用inspect元素时,我可以看到我试图查找的元素

import pandas as pd
import requests
from bs4 import BeautifulSoup
import re

url = 'https://www.ncaagamesim.com/college-basketball-predictions.asp'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

table = soup.find('table')

# Get column names
headers = table.find_all('th')
cols = [x.text for x in headers]

# Get all rows in table body
table_rows = table.find_all('tr')

rows = []
# Grab the text of each td, and put into a rows list
for each in table_rows[1:]:
    odd_avail = True
    data = each.find_all('td')
    time = data[0].text.strip()
    try:
        matchup, odds = data[1].text.strip().split('\xa0')
        odd_margin = float(odds.split('by')[-1].strip())
    except:
        matchup = data[1].text.strip()
        odd_margin = '-'
        odd_avail = False
    odd_team_win = data[1].find_all('img')[-1]['title']

    sim_team_win = data[2].find('img')['title']
    sim_margin = float(re.findall("\d+\.\d+", data[2].text)[-1])

    if odd_avail == True:
        if odd_team_win == sim_team_win:
            diff = sim_margin - odd_margin
        else:
            diff = -1 * odd_margin - sim_margin
    else:
        diff = '-'

    row = {cols[0]: time, 'Matchup': matchup, 'Odds Winner': odd_team_win, 'Odds': odd_margin,
           'Simulation Winner': sim_team_win, 'Simulation Margin': sim_margin, 'Diff': diff}
    rows.append(row)

df = pd.DataFrame(rows)
print (df.to_string())
# df.to_csv('odds.csv', index=False)

我在设置模拟团队赢线时出错。它正在获取数据[2],这是网站上的第三列,并查找img标题以获取团队名称。是因为img标题在另一个分区中吗?此外,当运行此代码时,它也不会打印出“赔率”列,该列存储在odd_margin变量中。设置该变量时是否存在错误?提前感谢您的帮助

至于未找到img标题,如果您查看新墨西哥州迪克西州的行,第三列中没有图像-源中也没有img标题


对于赔率列,在尝试/排除sim_team_win分配后,我得到表中的所有赔率值。

谢谢,我只添加了一次尝试,但刺激赢家除外,这解决了我的问题。谢谢你指出这一点。在sim团队赢得任务后获得所有赔率值是什么意思?没问题。我的意思是,在我修复了sim_team_win错误后,我看到了所有的赔率值。哦,好的,在修复sim_team win分配后,我仍然没有看到赔率值,您是否更改了其他内容?