Используя запросы Python, я не могу опубликовать данные - PullRequest
0 голосов
/ 21 мая 2018

Я хочу проанализировать данные сечения из x-com.Поэтому я использовал модуль запросов и пост.Я успешно публикую данные, включая данные об энергии.Если нет проблем, я могу получить данные сечения энергии 2 МэВ-20 МэВ.Но 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' put origin' https://physics.nist.gov/PhysRefData/Xcom/html/xcom1.html'>' https://physics.nist.gov/cgi-bin/Xcom/xcom2'

1 Ответ

0 голосов
/ 28 мая 2018

Это работает для меня.Установите энергию - умножьте на 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))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...