簡體   English   中英

使用請求獲取頁面而不是python中的源代碼,為什么? 我怎樣才能獲得源代碼?

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

頁面網址是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 -it is可能是您在Chrome中查看來源時所看到的該頁面的HTML內容。 但是, requests不會在響應中解析和執行Javascript,因為它不是瀏覽器,因此它只是為您提供文字響應文本。

就那么簡單。

編輯:實際上你的問題(特別是標題)是如此糟糕(在一般的清晰度和你非常差的英語語法方面),我不確定這是你唯一要求的,所以請嘗試在將來更加努力地寫下你的問題。

PS。 使用window.location = ...或類似技巧進行重定向的頁面很難在不使用完整瀏覽器或瀏覽器模擬器的情況下進行爬網。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM