试图解析网页时发生Python错误重定向

2024-04-25 14:25:53 发布

您现在位置:Python中文网/ 问答频道 /正文

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.animeplus.tv/anime-show-list/")
content =(html.read())
soup = BeautifulSoup(content)
print(soup.prettify())

该脚本可以与其他网页配合使用,但我会为我的目标网站运行该程序

<meta .$_server["request_uri"]."'"="" content="0;URL='" http-equiv="refresh"/>

我并不真正理解html代码

我认为这是某种重定向或防止网络抓取的方法

python有没有办法在重定向后访问代码,或者浏览器会以某种方式返回源代码

谢谢大家!


Tags: 代码fromimporthttprequesthtmlwwwcontent
1条回答
网友
1楼 · 发布于 2024-04-25 14:25:53

这里的技巧是页面重定向到自身并设置Cookie头,这很重要,没有它,您将无法获得在浏览器中看到的HTML

下面是使用^{}的解决方案-在同一个session中打开同一页:

import requests
from bs4 import BeautifulSoup

url = "http://www.animeplus.tv/anime-show-list/"
session = requests.session()
session.get(url)
response = session.get(url)  # open up the page second time
soup = BeautifulSoup(response.content)
print(soup.title.text)  # prints: "Watch Anime | Anime Online | Free Anime | English Anime | Watch Anime Online - AnimePlus.tv"

或者,您可以使用^{},但目前它不支持python 3。下面是它的工作原理:

>>> import mechanize
>>> browser = mechanize.Browser()
>>> browser.open('http://www.animeplus.tv/anime-show-list/')
>>> print browser.response().read()
<!DOCTYPE html>
<html>
<head>
  <title>Watch Anime | Anime Online | Free Anime | English Anime | Watch Anime Online - AnimePlus.tv</title> 
...

相关问题 更多 >

    热门问题