使用BeautifulSoup(bs4)提​​取表信息

时间:2013-07-26 07:42:14

标签: python

有人可以给我一段BeautifulSoup代码,以提取here表中的一些项目吗?

这是我的尝试:

from bs4 import BeautifulSoup
from urllib2 import urlopen

url = "http://biology.burke.washington.edu/conus/accounts/../recordview/record.php?ID=1ll&tabs=21100111&frms=1&res=&pglimit=A"

html = urlopen(url).read()
soup = BeautifulSoup(html,"lxml")
tables = soup.findAll("table")

然而,这是失败的 - 表结果是空的。

抱歉,我是BeautifulSoup noob。

谢谢!

2 个答案:

答案 0 :(得分:1)

给定的url页面不包含源代码中的任何表元素。

表是由iframe内的javascript 生成的

import urllib
from bs4 import BeautifulSoup

url = 'http://biology.burke.washington.edu/conus/recordview/description.php?ID=1l9l0l421l55llll&tabs=21100111&frms=1&pglimit=A&offset=&res=&srt=&sql2='

html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
tables = soup.find_all('table')
#print(tables)

硒溶液:

from selenium import webdriver
from bs4 import BeautifulSoup

url = "http://biology.burke.washington.edu/conus/accounts/../recordview/record.php?ID=1ll&tabs=21100111&frms=1&res=&pglimit=A"

driver = webdriver.Firefox()
driver.get(url)
driver.switch_to_frame(driver.find_elements_by_tag_name('iframe')[0])
soup = BeautifulSoup(driver.page_source)
tables = soup.find_all('table')
#print(tables)
driver.quit()

答案 1 :(得分:1)

这是我目前的工作流程:

from bs4 import beautifulsoup
from urllib2 import urlopen
url = "http://somewebpage.com"
html = urlopen(url).read()
soup = BeautifulSoup(html)
tables = soup.find_all('table')