【问题标题】:Trying to fetch data using payload post requests尝试使用有效负载发布请求获取数据
【发布时间】:2019-07-24 09:46:14
【问题描述】:
from bs4 import BeautifulSoup
import requests
url = 'https://hmbup.in/online/frmViewCandidateDetails.aspx'

html = requests.get(url).text
soup = BeautifulSoup(html, 'html.parser')

VIEWSTATEGENERATOR = soup.find(id='__VIEWSTATEGENERATOR')['value']
EVENTVALIDATION = soup.find(id='__EVENTVALIDATION')['value']
data ={
     '__VIEWSTATEGENERATOR': VIEWSTATEGENERATOR,
     '__EVENTVALIDATION': EVENTVALIDATION,
     'txtRegNo': 'H010002',
     'btnSearch': 'Search',
          }
r1 = requests.post(url,data=data)
soup1 = BeautifulSoup(r1.text,'html.parser')
name = soup1.find('span',id_='lblEngName')
print name.text

尝试使用有效负载发布请求来抓取此网络,但我没有得到结果。

【问题讨论】:

  • 也许您可以显示正在输出的内容以及预期的输出内容

标签: python web-scraping beautifulsoup python-requests


【解决方案1】:

你忘记了 __VIEWSTATE

from bs4 import BeautifulSoup
import requests

url = 'https://hmbup.in/online/frmViewCandidateDetails.aspx'

with requests.Session() as s:
    html = s.get(url).text
    soup = BeautifulSoup(html, 'html.parser')
    VIEWSTATE = soup.find(id='__VIEWSTATE')['value']
    VIEWSTATEGENERATOR = soup.find(id='__VIEWSTATEGENERATOR')['value']
    EVENTVALIDATION = soup.find(id='__EVENTVALIDATION')['value']
    data ={
         '__VIEWSTATEGENERATOR': VIEWSTATEGENERATOR,
         '__VIEWSTATE' : VIEWSTATE,
         '__EVENTVALIDATION': EVENTVALIDATION,
         'txtRegNo': 'H010002',
         'btnSearch': 'Search',
              }
    r1 = s.post(url,data=data)
    soup = BeautifulSoup(r1.content, 'lxml')
    table = soup.select_one('.j_table')

【讨论】:

  • 如果我必须提取范围 H010001 到 H010100@QHarr,你能告诉我如何使用 count 吗
【解决方案2】:

实现相同的方式略有不同。

import requests
from bs4 import BeautifulSoup

link = 'https://hmbup.in/online/frmViewCandidateDetails.aspx'

res = requests.get(link)
soup = BeautifulSoup(res.text, 'lxml')
payload = {item['name']:item.get('value','') for item in soup.select('input[name]')}
payload['txtRegNo'] = 'H010002'
resp = requests.post(link,data=payload)
soup_obj = BeautifulSoup(resp.text, 'lxml')

for trs in soup_obj.find(class_='j_table').find_all('tr'):
    data = [td.get_text(strip=True) for td in trs.find_all('td')]
    print(data)

【讨论】:

  • 是的,它可以工作,但如果我必须使用多个 RegNo@SIM,你能告诉我如何在其中使用 count
  • 是的,但如果我必须将范围设置为 1 到 1000,那么该怎么办? @SIM
  • 请更新您的问题,创建一个新帖子,描述要满足的其他要求,因为您最初提出的问题已经得到解答。谢谢。
  • 通过使用列表理解和名称属性来减少代码。 +
猜你喜欢
  • 1970-01-01
  • 2021-12-06
  • 2020-04-30
  • 1970-01-01
  • 2022-06-24
  • 1970-01-01
  • 2018-10-14
  • 2020-02-07
  • 1970-01-01
相关资源
最近更新 更多