我想解析x-com的横截面数据。所以我使用了request、module和post。我成功地发布了包括能源数据在内的数据。如果没有问题,我可以得到2 2Mev 20 2Mev能量的截面数据。但是x-com显示了0.001 me 10000 me的横截面数据。有什么问题吗?
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’
发布于 2018-05-28 18:16:37
这对我很有效。在有效载荷中设置能量乘以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))
https://stackoverflow.com/questions/50453008
复制相似问题