网络爬虫高级技巧:使用代理和反爬虫机制(版本:py3)——学习笔记

版权声明:自学笔记,如有引用请标明博客,感谢 https://blog.csdn.net/feng_jlin/article/details/82218819

 
  1. #1.设定程序休止时间

  2. import time

  3. time.sleep(5) #sleep for 5 seconds

 
  1. #2.设定代理,比如fb和微博等一些成熟的网站检测严格,会封锁IP

  2. #使用urllib.request的两个方法进行代理的设置

  3. proxy = urlrequest.ProxyHandler({'https': '47.91.78.201:3128'})#服务器地址

  4. opener = urlrequest.build_opener(proxy)

360浏览器
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE) 

扫描二维码关注公众号,回复: 3277657 查看本文章
opener.addheaders = [('User-Agent','...')]

设定User-Agent是为了告诉浏览器我是用的浏览器,而不是Python,模拟浏览器使用;如果不设定则浏览器知道你是Python,可能会限制访问速率等等

 
  1. import urllib.request as urlrequest

  2. import time #休息时间

  3. import random #为了时间随机

  4.  
  5. IMG_PATH = 'C:/Users/feng_jlin/Desktop/imgs/{}.jpg' #最后下载的图片放到imgs下,命名为{}

  6. DATA_FILE = 'C:/Users/feng_jlin/Desktop/data/votes.csv' #数据文件存在DATA下

  7. STORED_IMG_ID_FILE = 'C:/Users/feng_jlin/Desktop/data/cached_img.txt' #记录下载了哪些图片,目的是为了如果下载图片中断的时候,检测以前图片ID是否被下载,如果被下载过则不进行下载

  8. STORED_IMG_IDS = set() #将STORED_IMG_ID_FILE读取到这个集合中,用于检测

  9.  
  10. IMG_URL = 'https://maps.googleapis.com/maps/api/streetview?size=400x300&location={},{}' #谷歌map针对下载街景地图的一个API,后面两个{}则为经纬度

  11.  
  12. #creat the object,assign it to a variable

  13. proxy = urlrequest.ProxyHandler({'https': '47.91.78.201:3128'})

  14. #construct a new opener using your proxy settings

  15. opener = urlrequest.build_opener(proxy)

  16. opener.addheaders = [('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; 360SE)')]

  17. #install the openen on the module-level

  18. urlrequest.install_opener(opener)

  19.  
  20. #将上面记录已经下载的图片ID,cached_img.txt的文件,读取到STORED_IMG_IDS的集合中

  21. with open(STORED_IMG_ID_FILE) as input_file:

  22. for line in input_file:

  23. STORED_IMG_IDS.add(line.strip())

  24.  
  25. with open(DATA_FILE) as input_file:

  26. skip_first_line = True #一行一行读取数据,但是第一行是属性名称,所以设置这个变量进行初始化为TURE

  27. for line in input_file: #判断第一行是否为TRUE

  28. if skip_first_line:

  29. skip_first_line = False

  30. continue #如果进来第一句为TURE,则跳出这个循环不进行下一段程序,进入下次循环;并且skip_first_line改为False,以后可以进行下一段程序

  31. left_id, right_id, winner, left_lat, left_long, right_lat, right_long, category = line.split(',') #根据逗号切割数据,前面为切割出来的变量

  32.  
  33. if left_id not in STORED_IMG_IDS: #判断前面切割出来当个图片的left_id,是否包含在STORED_IMG_IDS中

  34. print ('saving img {}...'.format(left_id)) #正在下载的提示信息

  35. urlrequest.urlretrieve(IMG_URL.format(left_lat, left_long), IMG_PATH.format(left_id)) #这是一个urlretrieve方法,其实和Open与write一样,这个是将打开一个地址直接存储到一个路径里

  36. STORED_IMG_IDS.add(left_id) #在STORED_IMG_IDS字典中增加下载的left_id

  37. with open(STORED_IMG_ID_FILE, 'a') as output_file: #将下载的left_id增加到STORED_IMG_ID_FILE文件中,a为追加

  38. output_file.write('{}\n'.format(left_id))

  39. time_interval = random .uniform(1,5) #随机1-5秒停止

  40. time.sleep(time_interval) # wait some time, trying to avoid google forbidden (of crawler)

  41.  
  42. if right_id not in STORED_IMG_IDS: #同理为right_id照片

  43. print ('saving img {}...'.format(right_id))

  44. urlrequest.urlretrieve(IMG_URL.format(right_lat, right_long), IMG_PATH.format(right_id))

  45. STORED_IMG_IDS.add(right_id)

  46. with open(STORED_IMG_ID_FILE, 'a') as output_file:

  47. output_file.write('{}\n'.format(right_id))

  48. time_interval = random .uniform(1,5) #随机1-5秒停止

  49. time.sleep(time_interval) # wait some time, trying to avoid google forbidden (of crawler)

需要手动建文件夹

txt为手动建,csv为 http://pulse.media.mit.edu/data/ 下载的资料

猜你喜欢

转载自blog.csdn.net/hzp666/article/details/82257909