Write a web crawler in Python-zero-based 3 write ID traversal crawler

Source: Internet
Author: User

 when we visited the site, we found that some of the page IDs were numbered sequentially, and we could crawl the content using ID traversal. But the limitation is that some ID numbers are around 10 digits, so the crawl efficiency will be very low and low! 



Import itertools
from common import download


def iteration ():
Max_errors = 5 # Maximu M number of consecutive download errors allowed
Num_errors = 0 # Current number of consecutive download errors
For page in Itertools.count (1):
URL = ' http://example.webscraping.com/view/-{} '. Format (page)
HTML = Download (URL)
If HTML is None:
# received a error trying to download this webpage
Num_errors + = 1
If num_errors = = max_errors:
# Reached maximum amount of errors in A row so exit
Break
# So assume has reached the last country ID and can stop Downloadin G
Else:
# Success-can Scrape the result
# ...
Num_errors = 0

Write web crawler in Python-zero-based 3 write ID traversal crawler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.