gpt4 book ai didi

python - 无法使用发布请求从某些搜索结果中解析表格内容

转载 作者:行者123 更新时间:2023-12-03 16:48:19 26 4
gpt4 key购买 nike

我正在尝试从 a webpage 获取一些表格内容使用下面的脚本。要手动填充内容,必须从 this image 中显示的下拉列表中选择选项。在击中 Submit 之前按钮。我试图相应地模仿 post http 请求。但是,我可能出错了,这就是脚本不起作用的原因。具体来说,this is我想获取什么。
这是我尝试过的方式:

import requests
from bs4 import BeautifulSoup

URL = 'https://www.lgindiasocial.com/microsites/brand-store-web-five/locate.aspx'

headers = {
'x-microsoftajax': 'Delta=true',
'origin': 'https://www.lgindiasocial.com',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'referer': 'https://www.lgindiasocial.com/microsites/brand-store-web-five/locate.aspx',
}

with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'
r = s.get(URL)
soup = BeautifulSoup(r.text,"lxml")
payload = {i['name']:i.get('value','') for i in soup.select('input[name]')}
payload['ScriptManager1'] = 'UpdatePanel1|btnsubmit'
payload['ddlState:'] = 'Assam'
payload['ddlCity'] = 'Golaghat'
payload['ddllocation'] = 'Golaghat'
s.headers.update(headers)
r = s.post(URL,data=payload)
soup = BeautifulSoup(r.text,"lxml")
item = soup.select_one("table")
print(item)
当我运行脚本时,我得到 None 作为输出。
如何使用发布请求从搜索结果中获取表格内容?
编辑:如果我复制 the content直接来自开发工具的有效载荷并在有效载荷中使用相同的我得到了想要的结果。
import requests
from bs4 import BeautifulSoup

URL = 'https://www.lgindiasocial.com/microsites/brand-store-web-five/locate.aspx'

payload = "ScriptManager1=UpdatePanel1%7Cbtnsubmit&hidcity=&ddlState=Assam&ddlCity=Golaghat&ddllocation=Golaghat&__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=M%2BqldpZhV90EX2sawXMrHD7jYtOMXnrPuP8XfVtS21GK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uJceU%2BvO4U5E29ZPqBIolidYtKKH7lnRxKNk2BHtY93VNHPZEjTEDnHcGbgtHmxlBjHRQZlzJKWTjY5ccdFABihGx%2FzY0VCwaehpx2BWxy5qXqW1fX7e5uxxxHteYVt7YyrzYPsX%2B%2FlKiYwt23fsJzmmVkHwmu5%2FTSk1Ms9yJmBE%2B8pEF%2Bum01L8jRH4zxyTaD4s779uLZwLAUUzpi5cfseKTrjGv7uNjCpNci9BXbSdCdqrKa8aPiJX0lWUH9zid%2B8Jc7Jhx%2Bb6nzJpbZ8E9sPpUlcHVGUSzqixsiK91W%2FDDk2LCOvTqJJ9JXmy5cwRhL9r95okWq%2BDImTetFhdYk9%2F9VH3JsACpv4dqqdviEjjFpvmEp7SBMLSWw7toPUIRortPtriz3u9velTqNpHgmbmig8Znb%2F4Q8JrYfjPZzfRxN%2FuQXQyxUNUY2IsYbC5Bm7JWTMZe869muBdE%2FlMLujUkOFCXaOwZXuZHbr7neq0nro3RvYUggBLqxGFlG1Bp52iDNklcx8nfjVMOhOybfCMcxz6mq4Ew2hdLv4IslLRawI5u%2FPQe0vu0TG9LeBeR6Ok1sf72rWpvhD6yl4GTy8oJC1UglabWo8i5aMprxxAWuz%2BzLzizI3aRTQsl1MFKsD9gIGZsaFNAIb7gEgFgw%2B%2BSjTGR51mGES3sOUYXscIJVBciBs3F9vnr8u5gfKD3hLwqvc4djKMBxVQfjLEs%2FQwb7mlOx8XodaV6uOrkiZpw2WZNja5RPBIp4VXeXKXIxqBNsNA4eGT%2Bx2b2JadVB8%3D&__VIEWSTATEGENERATOR=06ED1D24&__VIEWSTATEENCRYPTED=&__ASYNCPOST=true&btnsubmit=Submit"

with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'
s.headers.update({'content-type':'application/x-www-form-urlencoded; charset=UTF-8'})
r = s.post(URL,data=payload)
soup = BeautifulSoup(r.text,"lxml")
item = soup.select_one("table")
print(item)

最佳答案

首先是你的代码中的一个小错字,(那里有额外的冒号)

payload['ddlState:'] = 'Assam'
更大的问题与页面的构造方式有关。该页面有三个下拉菜单,这些下拉菜单发送一个 POST 请求。每个 POST 请求都返回一个修改后的 __VIEWSTATE,需要包含在后续请求的 header 中。
在您的代码中,您仅从原始 GET 请求的 input[form] 中获取 __VIEWSTATE,您需要从最后一个 POST 请求中获取 __VIEWSTATE。所以以下应该工作:
with requests.Session() as s:
s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'
r = s.get(URL)
soup = BeautifulSoup(r.text, "lxml")

# first POST = Select State
payload = {i['name']: i.get('value', '') for i in soup.select('input[name]')}
payload['ScriptManager1'] = 'UpdatePanel1|btnsubmit'
payload['ddlState'] = 'Assam'
payload['ddlCity'] = 'Select City'
payload['ddllocation'] = 'Select Location'
payload['__EVENTTARGET'] = 'ddlState'
r = s.post(URL, data=payload)
soup = BeautifulSoup(r.text, "lxml")

# second POST = Select City
payload = {i['name']: i.get('value', '') for i in soup.select('input[name]')}
payload['ScriptManager1'] = 'UpdatePanel1|btnsubmit'
payload['ddlCity'] = 'Golaghat'
payload['ddllocation'] = 'Select Location'
payload['__EVENTTARGET'] = 'ddlCity'
r = s.post(URL, data=payload)
soup = BeautifulSoup(r.text, "lxml")

# third POST = Select Location
payload = {i['name']: i.get('value', '') for i in soup.select('input[name]')}
payload['ScriptManager1'] = 'UpdatePanel1|btnsubmit'
payload['ddlCity'] = 'Golaghat'
payload['ddllocation'] = 'Golaghat'
payload['__EVENTTARGET'] = ''

s.headers.update(headers)
r = s.post(URL, data=payload)
soup = BeautifulSoup(r.text, "lxml")
item = soup.select_one("table")
print(item)
这段代码有一些优化的空间。我试图让问题变得透明。

关于python - 无法使用发布请求从某些搜索结果中解析表格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63098307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com