How to implement page turning in python crawler

Release: 2019-07-08 09:13:27
Original
19901 people have browsed it

How to implement page turning in python crawler

Many friends who are new to python crawlers will encounter the problem of page turning in python crawlers. I will introduce one here.

The website that needs to be crawled is as shown in the picture

How to implement page turning in python crawler

There are four pages of this plant queried. When we usually turn pages, our first thought is to click on the next page on the page. The same is true when writing a crawler. We want to extract links on the page for access. But doing so is cumbersome and inefficient.

There are generally two ways to turn pages:

1. Observe the link changes when the website turns pages

2. If the request method of writing a crawler is a post request, then It is necessary to observe whether the changes in post data are regular.

Today I will talk about the first method

As shown in the picture, when I click on the next page, I observe the link change in the address bar

How to implement page turning in python crawler

We can be surprised to find that there is a page keyword in the link, and it happens to be 2. Let’s test it and change the value of page in the link to 3, and find that the access is normal. It's exactly the third page. Therefore, we found a way to turn the page and change the value of the keyword in the link.

So the steps of the page turning process are:

1. Get the total number of web pages

2. Use the for loop to change the value of page to turn the page.

The specific page turning code is implemented as follows:

Get the total page number

How to implement page turning in python crawler

Network request method

How to implement page turning in python crawler

Change the page value in the request header

How to implement page turning in python crawler

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

The above is the detailed content of How to implement page turning in python crawler. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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