Python selenium 靓汤:PHP重定向会从URL中删除有用的信息,如何修复?

kuuvgm7e  于 2023-02-12  发布在  Python
关注(0)|答案(2)|浏览(93)

我正在尝试使用Python Selenium和BeautifulSoup从一个支持PHP的网站上获取数据。
但网站会立即进行重定向:

<html>
<head>
<meta content="0;url=index.php" http-equiv="refresh"/>
</head>
<body>
<p>Redirecting to <a href="index.php">TestRail</a> ..</p>
</body>
</html>

......当我给予网址“https://mysite.thing.com“时
当我将其更改为:“https://mysite.thing.com/index.php“...我得到了一个404错误。
如何解决这个问题?欢迎提出任何建议!

wooyq4lh

wooyq4lh1#

我认为这是因为php请求的网页是用一个随机生成的令牌动态生成的,因此直接进入index.php会把你带到这里,因为你的“令牌”是nil,我会在selenium中经历一些动作来导航页面,就好像你正在做它一样,而不是试图向前跳过。
我可能是完全错误的php的事情顺便说一句,这是一个模糊的记忆...

cyej8jka

cyej8jka2#

使用以下更简单的代码就可以了:

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

import ssl

ssl._create_default_https_context = ssl._create_unverified_context



urlpage = "https://my.site.com"

print(urlpage)

driver.get(urlpage)
html = driver.page_source
print(html)

这遵循重定向,并做了我所期望的。

相关问题