使用Python请求提交表单数据POST不起作用
我正在编写一个python脚本来自动检查狗的重新归巢站点,当它们可用时,我们可能会采用这些站点,但是我无法完成这个站点上的表单数据,也不知道为什么 表单属性声明它应该有一个post方法,我已经检查了表单的所有输入并创建了一个有效负载 我希望返回带有搜索结果的页面,并从结果页面中删除html,这样我就可以开始处理它,但删除的只是表单页面,从来没有结果 我尝试使用.get和payload作为params,使用url和payload,并使用requests html库来呈现任何java脚本元素,但没有成功 如果您将url w_负载粘贴到浏览器中,它将加载页面并显示其中一个字段为空。如果在url栏中再次按enter键重新加载页面,而不修改页面加载的url。。。也许和饼干有关使用Python请求提交表单数据POST不起作用,python,python-3.x,web-scraping,Python,Python 3.x,Web Scraping,我正在编写一个python脚本来自动检查狗的重新归巢站点,当它们可用时,我们可能会采用这些站点,但是我无法完成这个站点上的表单数据,也不知道为什么 表单属性声明它应该有一个post方法,我已经检查了表单的所有输入并创建了一个有效负载 我希望返回带有搜索结果的页面,并从结果页面中删除html,这样我就可以开始处理它,但删除的只是表单页面,从来没有结果 我尝试使用.get和payload作为params,使用url和payload,并使用requests html库来呈现任何java脚本元素,但没有
import requests
from requests_html import HTMLSession
session = HTMLSession()
form_url = "https://www.rspca.org.uk/findapet?p_p_id=petSearch2016_WAR_ptlPetRehomingPortlets&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&_petSearch2016_WAR_ptlPetRehomingPortlets_action=search"
url_w_payload = "https://www.rspca.org.uk/findapet?p_p_id=petSearch2016_WAR_ptlPetRehomingPortlets&p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&_petSearch2016_WAR_ptlPetRehomingPortlets_action=search&noPageView=false&animalType=DOG&freshSearch=false&arrivalSort=false&previousAnimalType=&location=WC2N5DU&previousLocation=&prevSearchedPostcode=&postcode=WC2N5DU&searchedLongitude=-0.1282688&searchedLatitude=51.5072106"
payload = {'noPageView': 'false','animalType': 'DOG', 'freshSearch': 'false', 'arrivalSort': 'false', 'previousAnimalType': '', 'location': 'WC2N5DU', 'previousLocation': '','prevSearchedPostcode': '', 'postcode': 'WC2N5DU', 'searchedLongitude': '-0.1282688', 'searchedLatitude': '51.5072106'}
#req = requests.post(form_url, data = payload)
#with open("requests_output.txt", "w") as f:
# f.write(req.text)
ses = session.post(form_url, data = payload)
ses.html.render()
with open("session_output.txt", "w") as f:
f.write(ses.text)
print("Done")
有一些关于cookies和header的问题需要解决,但是一旦你做对了,你就会得到正确的回应 以下是如何做到这一点:
导入时间
从urllib.parse导入urlencode
导入请求
从bs4导入BeautifulSoup
查询字符串={
“p_p_id”:“petSearch2016_WAR_Ptlehomingportlets”,
“p_p_生命周期”:1,
“p_p_状态”:“正常”,
“p_p_模式”:“查看”,
“_petSearch2016_WAR_ptlehomingportlets_action”:“搜索”,
}
有效载荷={
'noPageView':'false',
“动物类型”:“狗”,
“freshSearch”:“false”,
'arrivalSort':'false',
“以前的动物类型”:“,
“位置”:“WC2N5DU”,
“以前的位置”:“,
“PreveSearchedPostcode:”,
'邮政编码':'WC2N5DU',
'搜索经度':'-0.1282688',
“搜索纬度”:“51.5072106”,
}
def make_cookie(cookie_dict:dict)->str:
返回“;”.join(cookie_dict.items()中k,v的f“{k}={v}”)
将requests.Session()作为连接:
主url=”https://www.rspca.org.uk"
connection.headers[“用户代理”]=“Mozilla/5.0(X11;Linux x86_64)”\
“AppleWebKit/537.36(KHTML,像壁虎一样)”\
“Chrome/90.0.4430.212 Safari/537.36”
r=connection.get(主url)
cookies=制作cookies(r.cookies.get\u dict())
附加字符串=f“cb enabled=enabled;”\
f“LFR_会话_状态_10110={int(time.time())}”
post_url=f“https://www.rspca.org.uk/findapet?{urlencode(查询字符串)}”
connection.headers.update(
{
“cookie”:cookies+附加的字符串,
“referer”:post_url,
“内容类型”:“应用程序/x-www-form-urlencoded”,
}
)
response=connection.post(post_url,data=urlencode(有效负载)).text
dogs=BeautifulSoup(响应,“lxml”)。查找所有(“a”,class=“detailLink”)
打印(“\n”.join(f”{main_url}{dog['href']}表示dog-in-dogs))
输出(为了简洁而缩短,并且在所有狗都响应时无需分页):
另外,我真的很喜欢这个挑战,因为我有两只狗来自一个庇护所。坚持下去,伙计 这看起来很棒,谢谢!迫不及待地想尝试一下……效果很好,我能够将它放入函数中并从主脚本调用。谢谢你的帮助!我的荣幸,史蒂夫。继续努力。
https://www.rspca.org.uk/findapet/details/-/Animal/JAY_JAY/ref/217747/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/STORM/ref/217054/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/DASHER/ref/205702/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/EVE/ref/205701/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/SEBASTIAN/ref/178975/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/FIJI/ref/169578/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/ELLA/ref/154419/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/BEN/ref/217605/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/SNOWY/ref/214416/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/BENSON/ref/215141/rehome/
https://www.rspca.org.uk/findapet/details/-/Animal/BELLA/ref/207716/rehome/
and much more ...