使用请求获取页面而不是python中的源代码,为什么?我怎样才能获得源代码?

时间:2013-10-26 07:40:19

标签: javascript python

页面网址为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>'

1 个答案:

答案 0 :(得分:2)

如果您查看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 - 这可能是您看到的那个页面的HTML内容在Chrome中查看来源。但是,requests不会在响应中解析和执行Javascript,因为它不是浏览器,因此它只是为您提供文字响应文本。

这很简单。

编辑:实际上你的问题(特别是标题)写得非常糟糕(就一般的清晰度和你非常差的英语语法而言),我不确定这是不是只有你问的是,所以请在将来努力写下你的问题。

使用window.location = ...或类似技巧进行重定向的

PS。页面很难在不使用完整浏览器或浏览器模拟器的情况下进行抓取。

相关问题