1. 程式人生 > >ID遍歷爬蟲

ID遍歷爬蟲

進行 error tool 我們 page 少見 break else errors

我們在目標網站打開時發現一些網頁ID是連續的數字時,這時候我們就可以用ID遍歷的方式進行爬取,但是這樣的網站弱點比較少見,特別是有一些ID數字是十多位的數字,這樣爬取的時候就會花大量的時間,所以說這樣的方法並不是最高效的方法


max_errors=5
# current number of consecutive download errors
num_errors=0
import itertools
for page in itertools.count(1):
url=‘http://example.webscraping.com/view/-%d‘ % page
html=download(url)
if html is None:
#received an error trying to download this page
num_errors+=1
if num_errors==max_errors:
# reached maximum number of
# consecutive errors so exit
break
else:
# sucess - can scrap the result
num_errors=0

ID遍歷爬蟲