Python Selenium,抓取网页JavaScript表
Python Selenium, scraping webpage javascript table
我将在下面的链接中废弃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()
# ============================================================
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
最简单的
方法可能是使用 Selenium 单击从 2-38 开始的lsm2
链接(因为存在 1 开始),然后在每次单击后用 id Match_Table
抓取表格 - 随时积累结果。
相关文章:
- 打开网页后立即获取网页的活动javascript函数
- Android键盘不适用于包含Javascript的网页
- 网页上失败的javascript会导致所有其他脚本失败
- 使用PowerShell在JavaScript网页上搜索文本字符串
- 等待Javascript网页抓取功能完成,然后再运行下一页
- 如何在Javascript网页中实现孤岛危机纳米服风格的菜单
- SeleniumjSoup从Javascript网页获取数据
- 从R中的javascript(网页)中删除xls文件
- 来自HTML/Javascript网页的Modbus TCP通信
- Javascript网页期望至少加载一个地图,但实际上可以'不要装任何东西
- 使用谷歌地图的javascript网页不能在Android上工作
- 如何使用Python与javascript网页交互
- 如何仅使用python标准库抓取javascript网页
- 在断开连接后检索html/javascript网页
- Javascript -网页内容字符串
- Javascript网页搜索定时器
- Javascript网页,下一张图片
- 用phantomjs渲染一个javascript网页页面
- Python - JavaScript 网页抓取与硒不能正常工作
- 从javascript网页中提取数据