【发布时间】:2021-05-23 13:14:59
【问题描述】:
我正在编写一个 python 脚本来自动检查狗重新归巢网站上我们可能会在它们可用时采用的狗,但是我无法完成此网站上的表单数据并且无法弄清楚为什么。
表单属性表明它应该有一个 post 方法,我已经检查了表单的所有输入并创建了一个有效负载。
我希望返回包含搜索结果的页面,并从结果页面中抓取 html,以便我可以开始处理它,但抓取的只是表单页面,永远不会有结果。
我尝试使用带有有效负载的 .get 作为参数,带有有效负载的 url 并使用 requests-html 库来呈现任何 Java 脚本元素,但均未成功。
如果您将 url_w_payload 粘贴到浏览器中,它会加载页面并显示其中一个字段为空。如果您然后在 url 栏中再次按 enter 以重新加载页面而不修改它加载的 url...可能与 cookie 有关?
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")
【问题讨论】:
标签: python python-3.x web-scraping