Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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
使用Python从web xhr提要中提取数据_Python_Python 3.x_Urllib_Bs4 - Fatal编程技术网

使用Python从web xhr提要中提取数据

使用Python从web xhr提要中提取数据,python,python-3.x,urllib,bs4,Python,Python 3.x,Urllib,Bs4,我正试图从别人那里得到网球比赛的结果。我特别想知道两名球员的名字,比赛的日期/时间和结果。我有两个问题: 默认情况下,该网页不会显示所有匹配项-只有单击页面底部的“显示更多匹配项”才能显示这些匹配项 当我在beautiful soup中加载html时,数据似乎不在那里。看起来它是由某种查询(“”)加载的,但我不知道如何直接运行它 我的代码示例如下: url="http://www.scoreboard.com/au/tennis/wta-singles/australian-open-2016/

我正试图从别人那里得到网球比赛的结果。我特别想知道两名球员的名字,比赛的日期/时间和结果。我有两个问题:

  • 默认情况下,该网页不会显示所有匹配项-只有单击页面底部的“显示更多匹配项”才能显示这些匹配项

  • 当我在beautiful soup中加载html时,数据似乎不在那里。看起来它是由某种查询(“”)加载的,但我不知道如何直接运行它

  • 我的代码示例如下:

    url="http://www.scoreboard.com/au/tennis/wta-singles/australian-open-2016/results/"
    
    from urllib.request import Request, urlopen
    req = Request(url, headers={"X-Fsign": "SW9D1eZo"})
    s = urlopen(req,timeout=50).read()
    s=urlopen(req, timeout=50).read()
    soup=BeautifulSoup(s, "lxml")
    
    match_times=soup.find_all("td", class_="cell_ad time")
    players=soup.find_all("span", class_="padl"
    results=soup.find_all("td", class_"cell_sa score  bold"
    #these all return empty element sets
    
    如何加载所有结果都可见的页面?如何优雅地提取上述数据

    编辑: 在建议使用selenium之后,我构建了一个函数,将使用selenium/Chrome加载页面,然后将html发送到bs4:

    def open_url(url):
        try:
            from urllib.request import Request, urlopen
            req = Request(url)
            s = urlopen(req,timeout=20).read()
            driver.get(url)
            try:
                driver.find_element_by_xpath("""//*[@id="tournament-page-results-more"]/tbody/tr/td/a""").click()
                time.sleep(5)
            except:
                print("No more results to show...")
            body=driver.find_element_by_id("fs-results")
            return BeautifulSoup(body.get_attribute("innerHTML"), "lxml")
        except:
            print("Webpage doesn't exist")
    
    这意味着我可以显示所有结果,但单击“显示更多”按钮。不幸的是,在页面正确加载之前,代码会继续运行,因此当我尝试获取包含结果的所有行时:

    matches=[]
    soup=open_url(url)
    rrows=soup.find_all("tr")
    for rrow in rrows:
        if rrow.attrs['class']!=['event_round']:
            matches.append(rrow)
    

    它只得到最初可见的结果。如何修复此问题?

    此页面使用JavaScript获取数据,如果使用
    urllib
    ,则只会获取没有数据的html代码


    使用Selenium刮取JS页面。

    此页面使用JavaScript获取数据,如果使用
    urllib
    ,则只会获取没有数据的html代码


    使用Selenium来清理JS页面。

    如果您有机会提供一个示例,我对SeleniumHave改编代码使用Selenium一点也不熟悉(见上文)。但仍然存在问题-页面在代码继续之前未完成加载…@user3725021您应该发布新问题,让社区解决您的问题,并接受我的回答以结束此问题。您是否有机会提供一个示例-我对SeleniumHave适配代码不太熟悉,无法使用selenium(见上文)。但仍然存在问题-在代码继续之前页面未完成加载…@user3725021您应该发布新问题,让社区解决您的问题,并接受我的回答以结束此问题。