2013-11-02 2 views
1

아래 링크 안에 자바 스크립트 테이블을 스크랩하려고합니다. 나는 웹 페이지의 내용을 얻을 때 http://data2.7m.cn/history_Matches_Data/2009-2010/92/en/index.shtmlPython Selenium, 웹 페이지 자바 스크립트 테이블을 스크랩

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 개의 축구 경기 테이블을 모두 얻는 방법을 알고 있습니까? 고맙습니다. 당신 같은 결과를 축적 - 다음 각 클릭 후 ID Match_Table와 테이블을 긁어 (1 시작할 존재하기 때문에)

# 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() 

답변

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 
0

가장 쉬운 일이 내게는 2-38에서 lsm2 링크를 클릭 셀레늄을 사용할 수 있습니다 가기.

+0

하지만 잉글랜드 축구 리그의 상당 부분이 폐기되어야하므로 많은 시간이 소요됩니다. 더 좋은 생각이야? :) –

관련 문제