python - 使用 Python 访问 Web 表 - NIST 网站

标签 python beautifulsoup urllib2 mechanize urllib

我正在尝试从 NIST 网站访问表格: http://physics.nist.gov/PhysRefData/XrayMassCoef/tab3.html

假设我单击元素锌。我想使用 python 2.7 将 Energy、u/p 和 u[en]/p 的信息检索到表的 3 列中。

我开始学习 BeautifulSoup 和 Mechanize。但是,我发现很难在与该网站上的表格相关的 HTML 代码中识别出清晰的模式。

我正在寻找的是这样的方法:

import mechanize
from bs4 import BeautifulSoup

page=mech.open("http://physics.nist.gov/PhysRefData/XrayMassCoef/tab3.html")
html = page.read()

soup = BeautifulSoup(html)

我的想法是尝试:

table = soup.find("table",...)

上面的...将是一些标识符。我在上面的 NIST 网站上找不到明确的标识符。

如何使用 python 2.7 导入此表?

编辑:是否可以将这 3 列放入表格中?

最佳答案

如果我能很好地理解你, 试试这个:

from bs4 import BeautifulSoup
import requests

respond = requests.get("http://physics.nist.gov/PhysRefData/XrayMassCoef/ElemTab/z30.html")
soup = BeautifulSoup(respond.text)
l = soup.find('table').find('table').find_all('tr')
for i in range(3 , len(l)):
    print l[i].get_text()

编辑: 其他方式(获取 ASCII 列)并将行放入列表 l:

from bs4 import BeautifulSoup
import requests

respond = requests.get("http://physics.nist.gov/PhysRefData/XrayMassCoef/ElemTab/z30.html")
soup = BeautifulSoup(respond.text)
l = soup.find('table').find('pre').get_text()[145:].split("\n")
print l

关于python - 使用 Python 访问 Web 表 - NIST 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25927679/

相关文章:

python - Nosetests 断言错误输出格式

python - 使用请求和BeautifulSoup获取Youtube视频标题有时会出错,有时会起作用(Python)

python - 正确的库用法 : requests w/bs4. BeautifulSoup?

python - 从 Python 中的 url 读取 gzip csv 时出错 : "_csv.Error: line contains NULL byte"

python-2.7 - Urllib2 响应 .read() 为空,即使响应不是

python - Pandas:要保存以包含数据类型的文件类型

python - 对列表进行排序并获取最常用的单词

python - X数组 : add a "layer" of data to NetCDF

python - 在特定标记后从 html 中提取所有文本?

python - 使用 urllib2 和 simplejson 无限循环