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?

Jun 28, 2019 pm 01:58 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

See all articles