Home > Backend Development > Python Tutorial > What should I do if the Python crawler encounters web page redirection when crawling the web page?

What should I do if the Python crawler encounters web page redirection when crawling the web page?

Release: 2019-07-08 09:12:03
Original
7872 people have browsed it

What should I do if the Python crawler encounters web page redirection when crawling the web page?

Search engine crawlers will encounter situations where the webpage is redirected when crawling the page. The so-called redirection (Redirect) is through various methods (these are 3 mentioned in this article) Redirect various network requests to other locations (URLs). The homepage of each website is the entrance to website resources. When redirection occurs on the homepage of the website, if it is not handled correctly, the content of the entire website is likely to be missed.

1. Server-side redirection is completed on the server side. Generally speaking, crawlers can adapt themselves and do not need special processing, such as response codes 301 (permanent redirection), 302 (temporary redirection), etc. . Specifically, it can be judged by the two attributes of url and status_code in the response object obtained by requests. When status_code is 301, 302 or other codes representing redirection, it means that the original request has been redirected; when the url attribute of the response object is inconsistent with the link when sending the request, it also means that the original request has been redirected and has been automatically processed.

#请求重定向
#方法一
response.setStatus(302);
response.setHeader("location", "/day06/index.jsp");
        
#方法二
response.sendRedirect("/day06/index.jsp");
Copy after login

scrapy shell Get the redirected page

scrapy shell -s ROBOTSTXT_OBEY=False --no-redirect " 
fetch(response.headers['Location'])
Copy after login

2. Meta refresh, that is, the tag in the web page declares the redirect link of the web page. This redirection is completed by the browser. , need to write code to process. For example, for a certain redirection, as shown in the comment in the third line of the HTML code below, the browser can automatically jump, but the crawler can only get the page before the jump and cannot automatically jump.

<html>
<head>  
  <meta http-equiv="refresh" content="0.1;url=http://www.redirectedtoxxx.com/"><!--本网页会在0.1秒内refresh为url所指的网页-->
</head>
</html>
Copy after login

The solution is to obtain the source code of the page before the jump and extract the redirect url information (the url attribute value in the third line of the above code). A specific operation:

①Use xpath('//meta[@http-equiv="refresh" and @content]/@content') to extract the value of content

②Use Regular expression extracts the redirected url value.

3. js redirection, redirection through JavaScript code. For example, the following JavaScript code

<script language=javascript>window.location.href=&#39;http://www.redirectedtoxxx.com&#39;</script>
Copy after login

is the easiest to solve when the web page contains content. Generally speaking, it is basically the content of the static web page that has been hard-coded, or the dynamic web page is rendered using a template, and the browser obtains the HTML It already contains all the key information, so the content you see directly on the web page can be loaded with javascript code through specific HTML tags. This situation is because although the content is in the HTML tag when the web page is displayed, in fact It is because the js code is executed and added to the tag, so at this time the content is in the js code, and the js execution is performed on the browser side, so when a program is used to request the web page address, the response obtained is the web page code and js code, so you can see the content on the browser side. Since the js is not executed during parsing, the content under the specified HTML tag must be found to be empty. The solution at this time is generally to find the js code containing the content. string, and then obtain the corresponding content through regular expressions instead of parsing HTML tags.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of What should I do if the Python crawler encounters web page redirection when crawling the web page?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template