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