首页 > 解决方案 > Python 网页抓取 - 为什么总是获取主页源代码?

问题描述

我已经完成了登录的所有初始步骤,能够访问该网站,我正在尝试使用 POST 请求模拟一个搜索操作(实际上是使用门户网站上的提交操作表单完成的),操作成功但页面源我得到的是来自“主页”页面。

我可以理解 100% 页面源代码是否未显示在页面源中(假设使用 生成的数据JavaScript),但这里的响应页面是静态“主页”页面。

什么是重定向到主页?如何停止导航?

以下是实现此目的的代码步骤:

import requests

session = requests.Session()
session.get(access_config['login-url'])
session.post(access_config["auth-url"], data=auth_config)
res = session.post(link)
print(res.status_code)
print(res.history)
print(res.url)
print(res.text)

回复:

status code :: 200

history :: <html><head><title>302 Moved Temporarily</title></head>
<body bgcolor="#FFFFFF">
<p>This document you requested has moved 
temporarily.</p>
<p>It's now at <a href="https://www.xxxx.com/a/welcome.html">https://www.xxxx.com/a/welcome.html</a>.</p>
</body></html>

response URL :: https://www.xxxx.com/a/welcome.html

我正在使用requests库,我知道很多人使用selenium库来进行这些操作,有什么我可以使用requests或除此之外的东西selenium吗?

标签: python-3.xwebweb-scrapingpython-requests

解决方案


@SonalBorkar 谢谢!再次

我已经使用“Firefox 检查器”进行了检查,并发现了所见背后的 2 个请求,您可以在图片中看到。 在此处输入图像描述

下面的代码完美运行 -

import requests

session = requests.Session()
session.get(access_config['login-url'])
session.post(access_config["auth-url"], data=auth_config)
session.post(link1)
res = session.get(link2)
print(res.status_code)
print(res.history)
print(res.url)
print(res.text)

link1link2双击检查员行后可见

link1--> 双击search.html

link2--> 双击seriessearch.html?...


推荐阅读