【问题标题】:don't know btnSearch.x and btnSearch.y value for post the Search Button不知道发布搜索按钮的 btnSearch.x 和 btnSearch.y 值
【发布时间】:2019-01-13 02:39:08
【问题描述】:

不知道 btnSearch.x 和 btnSearch.y 的值是否用于发布 Search Button 以单击具有以下参数的搜索按钮?

payload={
    'today':'20180806'
    'sortBy':'',
    'alertMsg':'', 
    'ddlShareholdingDay':'04',
    'ddlShareholdingMonth':'06', 
    'ddlShareholdingYear':'2018', 
    'btnSearch.x':'????',
    'btnSearch.y':'???'
}

import requests
from bs4 import BeautifulSoup
html = "url"
r=requests.post(html, data=payload)
c=r.content
soup=BeautifulSoup(c,"html.parser")

all_tables=[[td.text for td in tr.find_all('td')] for tr in 
soup.find_all('table')[2].find_all('tr')]
stock_info=[[sub_item.replace('\r\n', '') for sub_item in item] for item in all_tables]
for stock in stock_info[2:]:
    print stock

【问题讨论】:

  • 您没有在请求中使用payload。你试过这样做吗?会发生什么?
  • 亲爱的,是的,我不知道如何应用有效载荷来发出请求
  • 看看docs.python-requests.org/en/master/user/quickstart/…。但也请查看单击搜索按钮时浏览器发出的 POST 请求的详细信息。请求正文包含更多我不理解的数据。
  • 试试r=requests.post(html, data=payload)
  • 点击按钮?!您正在发送 HTTP 请求 - 您不能“点击”某些东西......如果您想进行真正的点击,您可以使用 Selenium,例如

标签: python beautifulsoup python-requests


【解决方案1】:

我已经使用curl 并能够使用这个简化的请求获得结果:

$ curl 'url' \
-H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/pdf;q=0.1' \
-H 'Accept-Language: de-DE,de;q=0.8,en-US;q=0.5,en;q=0.3' --compressed \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data 'today=20180806&ddlShareholdingDay=04&ddlShareholdingMonth=08&ddlShareholdingYear=2018&btnSearch.x=0&btnSearch.y=0'

请注意,此请求中没有 cookie。两个btnSearch 参数似乎是必需的,但我无法观察到不同值的任何影响。请求正文仅包含以下字段:

today=20180806
ddlShareholdingDay=04
ddlShareholdingMonth=08
ddlShareholdingYear=2018
btnSearch.x=0
btnSearch.y=0

使用 Python 并请求这样的请求可能如下所示:

import requests

url = "url"
payload =  {
  "today": "20180806",
  "ddlShareholdingDay": "04",
  "ddlShareholdingMonth": "08",
  "ddlShareholdingYear": "2018",
  "btnSearch.x": "0",
  "btnSearch.y": "0"
}
response = requests.post(url, data=payload)

然后您可以继续解析 HTML 正文 response.content。

【讨论】:

  • 亲爱的,谢谢,但是当我更改“ddlShareholdingDay”:“31”,“ddlShareholdingMonth”:“01”,“ddlShareholdingYear”:“2018”时,实际上没有任何变化,这意味着不要发布任何内容日月年信息,只使用默认值。
  • 好吧,我想你必须分析这个 HTML 表单的行为,并找出参数是如何工作的。我担心我们对此无能为力。也许您可以询问运行此网站的人员是否有可用的官方文档 API。也许它甚至会返回像 JSON 这样的结构化数据,这样您就不必抓取 HTML。
【解决方案2】:

btnSearch.x 和btnSearch.y 的值并不重要,它们只是btnSearch 图像的鼠标坐标(我认为),对POST 请求没有任何影响。

但是,ASP.NET Web 应用程序使用了一些重要的隐藏字段(__VIEWSTATE、__EVENTVALIDATION)。我们可以找到这些值并将它们与 POST 数据一起提交。

import requests
from bs4 import BeautifulSoup

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

data = {i['name']: i.get('value') for i in soup.select('input')}
data['ddlShareholdingDay'] = '04'
data['ddlShareholdingMonth'] = '06'
data['ddlShareholdingYear'] = '2018'
data['btnSearch.x'] = '????'
data['btnSearch.y'] = '???'

r = s.post(url, data)
soup = BeautifulSoup(r.text, 'html.parser')
stock_info = [
    [td.text.strip() for td in tr.find_all('td')] 
    for tr in soup.find_all('table')[2].find_all('tr')
]
for stock in stock_info[2:]:
    print(stock)

【讨论】:

    猜你喜欢
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    相关资源
    最近更新 更多