您的位置:首页 > 脚本大全 > > 正文

爬虫为什么被封了(详解爬虫被封的问题)

更多 时间:2021-10-14 00:35:07 类别:脚本大全 浏览量:1100

爬虫为什么被封了

详解爬虫被封的问题

如果你在爬虫过程中有遇到“您的请求太过频繁,请稍后再试”,或者说代码完全正确,可是爬虫过程中突然就访问不了,那么恭喜你,你的爬虫被对方识破了,轻则给予友好提示警告,严重的可能会对你的ip进行封禁,所以代理ip那就尤为重要了。今天我们就来谈一下代理ip,去解决爬虫被封的问题。

网上有许多代理ip,免费的、付费的。大多数公司爬虫会买这些专业版,对于普通人来说,免费的基本满足我们需要了,不过免费有一个弊端,时效性不强,不稳定,所以我们就需要对采集的ip进行一个简单的验证。

1.目标采集

本文主要针对西刺代理,这个网站很早之前用过,不过那个时候它还提供免费的api,现在api暂不提供了,我们就写个简单的爬虫去采集。

打开西刺代理,有几个页面,果断选择高匿代理。

爬虫为什么被封了(详解爬虫被封的问题)

chrome浏览器右键检查查看network,不难发现,每个ip地址都在td标签中,对于我们来说就简单许多了,初步的想法就是获取所有的ip,然后校验可用性,不可用就剔除。

 爬虫为什么被封了(详解爬虫被封的问题)

 定义匹配规则

  • ?
  • 1
  • 2
  • 3
  • 4
  • import re
  •  
  • ip_compile = re.compile(r'<td>(\d+\.\d+\.\d+\.\d+)</td>') # 匹配ip
  • port_compile = re.compile(r'<td>(\d+)</td>') # 匹配端口
  •  2.校验 这里我使用淘宝ip地址库检验可用性

    2.1、关于淘宝ip地址库

    目前提供的服务包括:

    1. 1. 根据用户提供的ip地址,快速查询出该ip地址所在的地理信息和地理相关的信息,包括国家、省、市和运营商。
    2. 2. 用户可以根据自己所在的位置和使用的ip地址更新我们的服务内容。

    我们的优势:

    1. 1. 提供国家、省、市、县、运营商全方位信息,信息维度广,格式规范。
    2. 2. 提供完善的统计分析报表,省准确度超过99.8%,市准确度超过96.8%,数据质量有保障。

    2.2、接口说明

    请求接口(get):
    ip.taobao.com/service/get…
    例:http://ip.taobao.com/service/getipinfo2.php?ip=111.177.181.44
    响应信息:
    (json格式的)国家 、省(自治区或直辖市)、市(县)、运营商
    返回数据格式:

  • ?
  • 1
  • 2
  • 3
  • 4
  • {"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
  • "region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
  • "country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
  • "county_id":"-1","isp_id":"100017"}}
  • 其中code的值的含义为,0:成功,1:失败。
    注意:为了保障服务正常运行,每个用户的访问频率需小于10qps。
    我们先通过浏览器测试一下

    输入地址http://ip.taobao.com/service/getipinfo2.php?ip=111.177.181.44

    爬虫为什么被封了(详解爬虫被封的问题)

    再次输入一个地址http://ip.taobao.com/service/getipinfo2.php?ip=112.85.168.98

    爬虫为什么被封了(详解爬虫被封的问题)

    代码操作

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • import requests
  •  
  • check_api = "http://ip.taobao.com/service/getipinfo2.php?ip="
  • api = check_api + ip
  • try:
  •   response = requests.get(url=api, headers=api_headers, timeout=2)
  •   print("ip:%s 可用" % ip)
  • except exception as e:
  •   print("此ip %s 已失效:%s" % (ip, e))
  •  3.代码
    代码中加入了异常处理,其实自己手写的demo写不写异常处理都可以,但是为了方便其他人调试,建议在可能出现异常的地方加入异常处理。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • import requests
  • import re
  • import random
  •  
  • from bs4 import beautifulsoup
  •  
  • ua_list = [
  •   "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/72.0.3626.109 safari/537.36",
  •   "mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, like gecko) chrome/62.0.3202.75 safari/537.36",
  •   "mozilla/5.0 (macintosh; intel mac os x 10_13_6) applewebkit/537.36 (khtml, like gecko) chrome/72.0.3626.119 safari/537.36",
  •   "mozilla / 5.0(windows nt 6.1;wow64) applewebkit / 537.36(khtml, likegecko) chrome / 45.0.2454.101safari / 537.36"
  •   ]
  •  
  •  
  • def ip_parse_xici(page):
  •   """
  •  
  •   :param page: 采集的页数
  •   :return:
  •   """
  •   ip_list = []
  •   for pg in range(1, int(page)):
  •     url = 'http://www.xicidaili.com/nn/' + str(pg)
  •     user_agent = random.choice(ua_list)
  •     my_headers = {
  •       'accept': 'text/html, application/xhtml+xml, application/xml;',
  •       'accept-encoding': 'gzip, deflate, sdch',
  •       'accept-language': 'zh-cn,zh;q=0.8',
  •       'referer': 'http: // www.xicidaili.com/nn',
  •       'user-agent': user_agent
  •     }
  •     try:
  •       r = requests.get(url, headers=my_headers)
  •       soup = beautifulsoup(r.text, 'html.parser')
  •     except requests.exceptions.connectionerror:
  •       print('connectionerror')
  •     else:
  •       data = soup.find_all('td')
  •       # 定义ip和端口pattern规则
  •       ip_compile = re.compile(r'<td>(\d+\.\d+\.\d+\.\d+)</td>') # 匹配ip
  •       port_compile = re.compile(r'<td>(\d+)</td>') # 匹配端口
  •       ips = re.findall(ip_compile, str(data)) # 获取所有ip
  •  
  •       ports = re.findall(port_compile, str(data)) # 获取所有端口
  •       check_api = "http://ip.taobao.com/service/getipinfo2.php?ip="
  •  
  •       for i in range(len(ips)):
  •         if i < len(ips):
  •           ip = ips[i]
  •           api = check_api + ip
  •           api_headers = {
  •             'user-agent': user_agent
  •           }
  •           try:
  •             response = requests.get(url=api, headers=api_headers, timeout=2)
  •             print("ip:%s 可用" % ip)
  •           except exception as e:
  •             print("此ip %s 已失效:%s" % (ip, e))
  •             del ips[i]
  •             del ports[i]
  •       ips_usable = ips
  •       ip_list += [':'.join(n) for n in zip(ips_usable, ports)] # 列表生成式
  •       print('第{}页ip采集完成'.format(pg))
  •   print(ip_list)
  •  
  •  
  • if __name__ == '__main__':
  •   xici_pg = input("请输入需要采集的页数:")
  •   ip_parse_xici(page=xici_pg)
  • 运行代码:

    爬虫为什么被封了(详解爬虫被封的问题)

    4.为你的爬虫加入代理ip

    建议大家可以把采集的ip存入数据库,这样每次爬虫的时候直接调用即可,顺便提一下代码中怎么加入代理ip。

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • import requests
  •  
  • url = 'www.baidu.com'
  • headers = {
  •   "user-agent": "mozilla/5.0 (macintosh; intel mac os x 10_13_6) applewebkit/537.36 (khtml, like gecko) chrome/72.0.3626.119 safari/537.36",
  • }
  • proxies = {
  •   "http": "http://111.177.181.44:9999",
  •   # "https": "https://111.177.181.44:9999",
  • }
  •  
  • res = requests.get(url=url, headers=headers, proxies=proxies)
  • 好了,妈妈再也不担心我爬虫被封了

    以上所述是小编给大家介绍的爬虫被封的问题详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对开心学习网网站的支持!

    原文链接:https://juejin.im/post/5cbe71c9f265da03a54c1d29

    标签:爬虫 被封
    您可能感兴趣