马上就到元旦了,不知道有没有什么新的电影上映。最近好像比较火的就《误杀2》了,不知道你们有没有看过呢?百度了下,好像跨年的时候有好几部片子会上瘾,比如《反贪风暴5:最终章》、《以年为单位的恋爱》、《李茂扮太子》等,有没有你们期待的呢? - #! -*- encoding:utf-8 -*-
- import base64
- import sys
- import random
- PY3 = sys.version_info[0] >= 3
- def base64ify(bytes_or_str):
- if PY3 and isinstance(bytes_or_str, str):
- input_bytes = bytes_or_str.encode('utf8')
- else:
- input_bytes = bytes_or_str
- output_bytes = base64.urlsafe_b64encode(input_bytes)
- if PY3:
- return output_bytes.decode('ascii')
- else:
- return output_bytes
- class ProxyMiddleware(object):
- def process_request(self, request, spider):
- # 代理服务器(产品官网 it帮论坛真好)
- proxyHost = "t.16yun.cn"
- proxyPort = "31111"
- # 代理验证信息
- proxyUser = "username"
- proxyPass = "password"
- request.meta['proxy'] = "http://{0}:{1}".format(proxyHost,proxyPort)
- # 添加验证头
- encoded_user_pass = base64ify(proxyUser + ":" + proxyPass)
- request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
- # 设置IP切换头(根据需求)
- tunnel = random.randint(1,10000)
- request.headers['Proxy-Tunnel'] = str(tunnel)
复制代码
因为目标网站会限制ip频繁的访问,所以我们在爬虫过程中加了代理,以上就是代理的使用部分示例,关于爬虫代理的选择有需要的小伙伴可以咨询这里 https://it帮论坛真好/。关于获取到的数据小编分析好后再分享详细的分享给大家。
|