【问题标题】:Python Selenium, scraping webpage javascript tablePython Selenium,抓取网页javascript表
【发布时间】:2013-11-13 08:55:00
【问题描述】:

我将废弃以下链接中的 javascript 表格。 http://data2.7m.cn/history_Matches_Data/2009-2010/92/en/index.shtml

import codecs
import lxml.html as lh
from lxml import etree
import requests
from selenium import webdriver
import urllib2
from bs4 import BeautifulSoup

URL = 'http://data2.7m.cn/history_Matches_Data/2009-2010/92/en/index.shtml'
profile = webdriver.FirefoxProfile()
profile.set_preference('network.http.max-connections', 30)
profile.update_preferences()
browser = webdriver.Firefox(profile)
browser.get(URL)
content = browser.page_source
soup = BeautifulSoup(''.join(content))

当我获得网页的内容后,我需要知道该特定联赛的足球比赛轮数。

下面的代码只找到了唯一的表,请问如何获得所有 38 个足球比赛的表?谢谢。

# scrap the round of soccer matches
soup.findAll('td', attrs={'class': 'lsm2'})

# print the soccer matches' result of default round, but there have 38 rounds (id from s1 to s38)
print soup.find("div", {"id": "Match_Table"}).prettify()

【问题讨论】:

    标签: javascript python selenium beautifulsoup


    【解决方案1】:
    # ============================================================
    import codecs
    import lxml.html as lh
    from lxml import etree
    import requests
    from selenium import webdriver
    import urllib2
    from bs4 import BeautifulSoup
    from pandas import DataFrame, Series
    import html5lib
    
    URL = 'http://data2.7m.cn/history_Matches_Data/2009-2010/92/en/index.shtml'
    profile = webdriver.FirefoxProfile()
    profile.set_preference('network.http.max-connections', 30)
    profile.update_preferences()
    browser = webdriver.Firefox(profile)
    browser.get(URL)
    
    content = browser.page_source
    soup = BeautifulSoup(''.join(content))
    # num = soup.findAll('td', attrs={'class': 'lsm2'})
    # num = soup.findAll('table')[2].findAll('td')[37].text
    # soup.findAll('table',attrs={'class':'e_run_tb'})
    
        num1 = soup.findAll('table')[2].findAll('tr')
        for i in range(1,len(num1)+1):
            for j in range(1,len(num1[i-1])+1):
                # click button on website
                clickme = browser.find_element_by_xpath('//*[@id="e_run_tb"]/tbody/tr'+'['+str(i)+']'+'/td'+'['+str(j)+']')
                clickme.click()
    
                content = browser.page_source
                soup = BeautifulSoup(''.join(content))
    
                table = soup.find('div', attrs={'class': 'e_matches'})
                rows = table.findAll('tr')
    #           for tr in rows:
    #             cols = tr.findAll('td')
    #             for td in cols:
    #                    text = td.find(text=True)
    #                    print text,
    #                print
                for tr in rows[5:16]: #from row 5 to 16
                    cols = tr.findAll('td')
                    for td in cols:
                        text = td.find(text=True)
                        print text,
                    print
                print
    

    【讨论】:

      【解决方案2】:

      最简单的方法可能是我使用 Selenium 单击 2-38 中的 lsm2 链接(因为存在 1 开始),然后在每次单击后刮掉 ID 为 Match_Table 的表——在你积累结果时去吧。

      【讨论】:

      • 但似乎很耗时,因为有很多英国足球联赛需要取消。有更好的主意吗? :)
      猜你喜欢
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2020-11-23
      • 2018-11-13
      相关资源
      最近更新 更多