首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python的请求,我不能发布数据

使用python的请求,我不能发布数据
EN

Stack Overflow用户
提问于 2018-05-22 00:29:04
回答 1查看 803关注 0票数 1

我想解析x-com的横截面数据。所以我使用了request、module和post。我成功地发布了包括能源数据在内的数据。如果没有问题,我可以得到2 2Mev 20 2Mev能量的截面数据。但是x-com显示了0.001 me 10000 me的横截面数据。有什么问题吗?

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2018-05-28 18:16:37

这对我很有效。在有效载荷中设置能量乘以10。查询端点,提取相关分片并保存到文件中。

非常基础。希望这能解决你的问题。

代码语言:javascript
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50453008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档