Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python错误-重定向尝试解析网页_Python_Html_Web Scraping - Fatal编程技术网

Python错误-重定向尝试解析网页

Python错误-重定向尝试解析网页,python,html,web-scraping,Python,Html,Web Scraping,该脚本可以与其他网页配合使用,但我会为我的目标网站运行该程序 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()) 我并不真正理

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

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())

我并不真正理解html代码

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

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


谢谢大家!

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

以下是使用的解决方案-在相同的
会话中打开相同的页面

<meta .$_server["request_uri"]."'"="" content="0;URL='" http-equiv="refresh"/>
或者,您可以使用,但目前它不支持python 3。下面是它的工作原理:

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"
导入机械化 >>>browser=mechanize.browser() >>>浏览器。打开('http://www.animeplus.tv/anime-show-list/') >>>打印browser.response().read() 观看动画|在线动画|免费动画|英语动画|在线观看动画-AnimePlus.tv ...
看起来源页面破坏了他们的PHP代码通过curl获取页面也会返回相同的响应——我尝试了以下重定向/更改用户代理,但没有成功:(
>>> 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> 
...