我想解析x-com的横截面数据。所以我使用了请求模块和帖子。我成功发布了包括能源数据在内的数据。如果没有问题,我可以获得2Mev-20Mev能量的横截面数据。但是x-com显示了0.001Mev-10000Mev的横截面数据。问题是什么?
import requests
from bs4 import BeautifulSoup
energy=2
with requests.Session() as session:
datas={'ZNum':31,
'NumAdd':1,
'OutOpt':'PIC',
'Graph6':'on',
'Output':'on',
'Resol':'-r72.73x72.73',
'WindowXmin':energy,
'WindowXmax':energy*10,}
res=session.post('https://physics.nist.gov/cgi-bin/Xcom/xcom3_1',data=datas)
soup=BeautifulSoup(res.text,'html.parser')
print(soup)
' https://physics.nist.gov/cgi-bin/Xcom/xcom3_1'放置来源' https://physics.nist.gov/PhysRefData/Xcom/html/xcom1.html'>' https://physics.nist.gov/cgi-bin/Xcom/xcom2'
答案 0 :(得分:0)
这对我有用。设置能量 - 在有效载荷中乘以10。查询端点,提取相关部分并存储在文件中。
非常基本。希望这能解决你的问题。
import requests
from bs4 import BeautifulSoup
# set the energy - you proposed a value of 2
energy = 2
# set the right payload - multiply the energymax by 10 - you wanted this
payload = {
"ZNum": 31,
"OutOpt": "PIC",
"Graph6": "on",
"NumAdd": 1,
"Output": "on",
"WindowXmin": energy,
"WindowXmax": energy*10,
"ResizeFlag": "on"
}
# define path to store data
path_to_output_file = "C:/myfile.txt"
# define the correct endpoint
url = "https://physics.nist.gov/cgi-bin/Xcom/xcom3_1"
# make the request to the endpoint
r = requests.post(url, data=payload)
# load the response into BeautifulSoup
soup = BeautifulSoup(r.text, "html.parser")
# identify the rows that hold your data (they share same bgcolor)
trs = soup.find_all('tr', attrs={'bgcolor': '#FFFFCC'})
# open a file to store the data
with open(path_to_output_file, 'w', encoding='utf-8') as f:
# loop through the rows and extract the values
for tr in trs:
tds = tr.find_all('td')
# list comprehension that finds and then joins the values of all columns by a semicolon
# the first td is blank and therefor omitted
data_line = ";".join([td.get_text() for td in tds[1:]])
# write the data to the file
f.write("{}\n".format(data_line))