代码之家  ›  专栏  ›  技术社区  ›  chitown88

靓汤-在特定页面有问题

  •  0
  • chitown88  · 技术社区  · 6 年前

    我在过去已经成功地使用了漂亮的汤(我还在学习如何使用它),但我现在正纠结于如何在这里获得这张特定的桌子:

    https://fantasydata.com/nfl-stats/point-spreads-and-odds?season=2017&seasontype=1&week=1

    在过去,这很简单:

    url = 'https://fantasydata.com/nfl-stats/point-spreads-and-odds?   season=2017&seasontype=1&week=1' 
    html = requests.get(url)
    soup = BeautifulSoup(html.text, "html.parser")
    

    driver = webdriver.Chrome()
    page_url = 'https://fantasydata.com/nfl-stats/point-spreads-and-odds?season=2017&seasontype=1&week=1' %(year,nfl_week)
    driver.get(page_url)
    soup = BeautifulSoup(driver.page_source, "lxml")
    

    但是表中的任何数据都不能从页面源进行解析。

    有没有更好的方法来处理这个美丽的汤?

    编辑:

    好吧,我回去做了:

    driver=webdriver.Chrome()
    页面url='https://fantasydata.com/nfl-stats/point-spreads-and-orks?赛季=2017赛季类型=1周=1%(年度,nfl周)
    driver.get(页面url)
    soup=BeautifulSoup(driver.page_source,“lxml”)
    

    再一次。这一次,数据出现了。我以为既然是装进去的,使用硒是正确的方法,但当它不起作用时就被扔掉了。

    你知道为什么第一次没用吗?在页面加载之前,我没有关闭浏览器或任何东西。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Roman Yakubovich    6 年前

    你不需要美容品或硒。数据作为python字典提供 POSTing 查询到 https://fantasydata.com/NFLTeamStats/Odds_Read .

    query = {  # just mimicking sample query that I saw after loading your link
        'page': 1,
        'pageSize': 50,
        'filters.season': 2017,
        'filters.seasontype': 1,
        'filters.week': 1,
    }
    response = requests.post('https://fantasydata.com/NFLTeamStats/Odds_Read', data=query)
    data = response.json()
    data
    {'Data': [{'Date': 'September 7, 2017 8:30 PM', 'Favorite': 'at Patriots', 'PointSpread': '-9.0', 'UnderDog': 'Chiefs', 'OverUnder': '48.0', 'AwayTeamMoneyLine': '+400', 'HomeTeamMoneyLine': '-450'}, {'Date': 'September 10, 2017 1:00 PM', 'Favorite': 'Buccaneers', 'PointSpread': '-2.5', 'UnderDog': 'at Dolphins', 'OverUnder': '41.5', 'AwayTeamMoneyLine': '-140', 'HomeTeamMoneyLine': '+120'}, {'Date': 'September 10, 2017 1:00 PM', 'Favorite': 'at ...  
    

    通过研究Chrome开发工具(push F12)的网络部分,特别是XHR小节,您可以找到这种方法: request data request response

        2
  •  1
  •   ajaysinghdav10d dhilt    6 年前

    好吧,所以我四处窥探,有人打我!!! 我做了与另一个答案相同的事情,但是在Firefox开发工具(组合ctrl+shift+k)中,使用 xhr “网络”选项卡的部分与另一个答案一样。看起来在站点上填充表的API调用是 POST 请求 https://fantasydata.com/NFLTeamStats/Odds_Read . 这是一个 js 具有所有可用参数的对象:

    {
    filter:,    
    filters.endweek:,
    filters.exportType:,
    filters.leaguetype:,
    filters.minimumsnaps:,
    filters.playerid:,
    filters.position:,
    filters.scope:,
    filters.scoringsystem:,
    filters.searchtext:,
    filters.season: 2017,
    filters.seasontype: 1,
    filters.startweek:,
    filters.stattype:,
    filters.subscope:,
    filters.team:,
    filters.teamaspect:,
    filters.week: 1,
    group:,
    page: 1,
    pageSize: 50,
    sort:
    }
    

    尸体 岗位 会是一个 json 像上面那个一样的物体。如果它们不阻止跨源请求,您可以直接使用Python请求库。如果它们确实阻止了跨源请求,您可以尝试模拟它们设置的头和选项,或者,我忘了怎么做了,但我知道您可以从页面向selenium注入javascript AJAX请求。作为旁注,你必须使用 webDriverWait 或者其他一些异步代码来等待响应,如果您想自动化异步 js公司 Python中的inection。