在 Python 中使用请求获取页面但不使用源代码,为什么?如何获取源代码

use requests get page but not source code in python, why? how can i get source code?

本文关键字:获取 源代码 为什么 何获取 Python 请求      更新时间:2023-09-26

页面网址www.nenu.edu.cn/intramural/content/news/110.php
使用 Chrome 获取源代码与使用请求不同。我使用 requests.get 是

u''r'n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'r'n<html xmlns="http://www.w3.org/1999/xhtml">'r'n<head>'r'n<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />'r'n<title>'u4e1c'u5317'u5e08'u8303'u5927'u5b66</title>'r'n<link href="../../images/nenu_news.css" rel="stylesheet" type="text/css" />'r'n</head>'r'n<body>'r'n'r'n<script language="javascript" type="text/javascript"> window.location ="http://www.nenu.edu.cn/intramural/content/news/110.php";</script>'

如果你查看requests.get给你的 HTML,你会看到以下代码片段:

<script language="javascript" type="text/javascript">
window.location ="http://www.nenu.edu.cn/intramural/content/news/110.php";
</script>

这意味着,当Chrome加载页面时,页面中的小Javascript代码片段会告诉浏览器导航到http://www.nenu.edu.cn/intramural/content/news/110.php - 这可能是您在Chrome中查看源代码时看到的该页面的HTML内容。但是,requests不会在响应中解析和执行 Javascript,因为它不是浏览器,所以它只是为您提供字面响应文本。

就这么简单。

编辑:实际上你的问题(尤其是标题)写得很糟糕(就一般清晰度以及你非常糟糕的英语语法而言),我不确定这是否是你唯一问的问题,所以请试着花更多的精力写你的问题在未来。

使用window.location = ...或类似技巧进行重定向的页面很难在不诉诸使用完整的浏览器或浏览器模拟器的情况下抓取。