Python3 爬虫使用User Agent和代理IP隐藏身份
2017-05-22 16:34
1141 查看
有一些网站不喜欢被爬虫程序访问,所以会检测连接对象,如果是爬虫程序,也就是非人点击访问,它就会不让你继续访问,所以为了要让程序可以正常运行,需要隐藏自己的爬虫程序的身份。此时就可以通过设置User Agent的来达到隐藏身份的目的,User Agent的中文名为用户代理,简称UA。
User Agent存放于Headers中,服务器就是通过查看Headers中的User Agent来判断是谁在访问。在Python中,如果不设置User Agent,程序将使用默认的参数,那么这个User Agent就会有Python的字样,如果服务器检查User Agent,那么没有设置User Agent的Python程序将无法正常访问网站。
常见的User Agent
AndroidMozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19
Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 Firefox
设置 User Agent ,方法一
在创建Request对象的时候,填入headers参数(包含User Agent信息),这个He
4000
aders参数要求为字典。from urllib import request
if __name__ == "__main__":
url = 'http://www.baidu.com'
head = {}
# 写入User Agent信息
head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19'
# 创建Request对象
req = request.Request(url, headers=head)
# 传入创建好的Request对象 req
response = request.urlopen(req)
# 读取响应信息并解码
html = response.read().decode('utf-8')
# 打印信息
print(html)
设置 User Agent ,方法二
在创建Request对象的时候不添加headers参数,在创建完成之后,使用add_header()的方法,添加headers。from urllib import request
if __name__ == "__main__":
url = 'http://www.baidu.com'
# 创建Request对象
req = request.Request(url)
# 传入headers
req.add_header('User-Agent','Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19')
# 传入创建好的Request对象 req
response = request.urlopen(req)
# 读取响应信息并解码
html = response.read().decode('utf-8')
# 打印信息
print(html)IP代理的使用
一个很简单的解决办法就是设置延时,但是这显然不符合爬虫快速爬取信息的目的,所以另一种更好的方法就是使用IP代理。
1/ 调用 urllib.request.PorxyHandler(),proxiex 参数为一个字典。
2/ 创建 Opener (类似 urlopen(),这个是自己定制的)
3/ 安装 Opener
使用install_opener方法之后,会将程序默认的urlopen方法替换掉。也就是说,如果使用install_opener之后,在该文件中,再次调用urlopen会使用自己创建好的opener。如果不想替换掉,只是想临时使用一下,可以使用opener.open(url),这样就不会对程序默认的urlopen有影响。
选取 IP 代理
在写代码之前,先在代理IP网站选好一个IP地址,推荐西刺代理IP。
西刺免费代理IP:http://www.xicidaili.com/
注意:当然也可以写个正则表达式从网站直接爬取IP,但是要记住不要太频繁爬取,加个延时什么的,太频繁给服务器带来压力了,服务器会直接把你block,不让你访问的。
从西刺网站选出信号好的IP,这里选择如下:(125.110.67.117:8998)
编写代码访问http://www.whatismyip.com.tw/,该网站是测试自己IP为多少的网址,服务器会返回访问者的IP。
代码如下:from urllib import request
if __name__ == "__main__":
url = 'http://www.whatismyip.com.tw/'
# 这里是从西刺获取的代理 IP
proxy = {'http': '125.110.67.117:8998'}
# 创建 ProxyHandler 对象
proxy_support = request.ProxyHandler(proxy)
# 创建 build_opener 对象
opener = request.build_opener(proxy_support)
# 添加 User Angent
opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36')]
# 安装 Opener
request.install_opener(opener)
# 使用安装好的Opener
response = request.urlopen(url)
# 读取响应信息并解码
html = response.read().decode('utf-8')
# 打印信息
print(html) 看执行后本地的 IP 已变为代理的 IP:
User Agent存放于Headers中,服务器就是通过查看Headers中的User Agent来判断是谁在访问。在Python中,如果不设置User Agent,程序将使用默认的参数,那么这个User Agent就会有Python的字样,如果服务器检查User Agent,那么没有设置User Agent的Python程序将无法正常访问网站。
常见的User Agent
AndroidMozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19
Mozilla/5.0 (Linux; U; Android 4.0.4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla/5.0 (Linux; U; Android 2.2; en-gb; GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 Firefox
Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0 Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0Google Chrome
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36 Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19iOS
Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3 Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A101a Safari/419.3这些 User Agent,直接 copy 就能用。
设置 User Agent ,方法一
在创建Request对象的时候,填入headers参数(包含User Agent信息),这个He
4000
aders参数要求为字典。from urllib import request
if __name__ == "__main__":
url = 'http://www.baidu.com'
head = {}
# 写入User Agent信息
head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19'
# 创建Request对象
req = request.Request(url, headers=head)
# 传入创建好的Request对象 req
response = request.urlopen(req)
# 读取响应信息并解码
html = response.read().decode('utf-8')
# 打印信息
print(html)
设置 User Agent ,方法二
在创建Request对象的时候不添加headers参数,在创建完成之后,使用add_header()的方法,添加headers。from urllib import request
if __name__ == "__main__":
url = 'http://www.baidu.com'
# 创建Request对象
req = request.Request(url)
# 传入headers
req.add_header('User-Agent','Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19')
# 传入创建好的Request对象 req
response = request.urlopen(req)
# 读取响应信息并解码
html = response.read().decode('utf-8')
# 打印信息
print(html)IP代理的使用
一个很简单的解决办法就是设置延时,但是这显然不符合爬虫快速爬取信息的目的,所以另一种更好的方法就是使用IP代理。
1/ 调用 urllib.request.PorxyHandler(),proxiex 参数为一个字典。
2/ 创建 Opener (类似 urlopen(),这个是自己定制的)
3/ 安装 Opener
使用install_opener方法之后,会将程序默认的urlopen方法替换掉。也就是说,如果使用install_opener之后,在该文件中,再次调用urlopen会使用自己创建好的opener。如果不想替换掉,只是想临时使用一下,可以使用opener.open(url),这样就不会对程序默认的urlopen有影响。
选取 IP 代理
在写代码之前,先在代理IP网站选好一个IP地址,推荐西刺代理IP。
西刺免费
注意:当然也可以写个正则表达式从网站直接爬取IP,但是要记住不要太频繁爬取,加个延时什么的,太频繁给服务器带来压力了,服务器会直接把你block,不让你访问的。
从西刺网站选出信号好的IP,这里选择如下:(125.110.67.117:8998)
编写代码访问http://www.whatismyip.com.tw/,该网站是测试自己IP为多少的网址,服务器会返回访问者的IP。
代码如下:from urllib import request
if __name__ == "__main__":
url = 'http://www.whatismyip.com.tw/'
# 这里是从西刺获取的代理 IP
proxy = {'http': '125.110.67.117:8998'}
# 创建 ProxyHandler 对象
proxy_support = request.ProxyHandler(proxy)
# 创建 build_opener 对象
opener = request.build_opener(proxy_support)
# 添加 User Angent
opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36')]
# 安装 Opener
request.install_opener(opener)
# 使用安装好的Opener
response = request.urlopen(url)
# 读取响应信息并解码
html = response.read().decode('utf-8')
# 打印信息
print(html) 看执行后本地的 IP 已变为代理的 IP:
相关文章推荐
- 鱼c笔记——Python爬虫(三):使用User Agent和代理IP隐藏身份
- Python3网络爬虫之使用User Agent和代理IP隐藏身份
- Python3-使用User Agent和代理IP隐藏身份
- Python3网络爬虫(四):使用User Agent和代理IP隐藏身份
- Python3网络爬虫(四):使用User Agent和代理IP隐藏身份
- Python3网络爬虫_使用User Agent和代理IP隐藏身份
- Python 爬虫入门(二)—— IP代理使用 - 不剃头的一休哥 - 博客园
- Python 爬虫之使用代理ip
- Python 爬虫入门—— IP代理使用
- Python 爬虫入门(二)—— IP代理使用
- [Python爬虫] 使用urllib2库检测代理ip是否可用,并使用代理提交GET、POST请求
- python3实现网络爬虫(7)-- 使用ip代理抓取网页
- Python 爬虫入门(二)—— IP代理使用
- 使用python为爬虫获取免费代理ip
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- Python:爬虫使用代理防封IP:HTTP Error 403: Forbidden
- 【Python3.6爬虫学习记录】(十一)使用代理IP及用多线程测试IP可用性--刷访问量
- [python]新手写爬虫v2.5(使用代理的异步爬虫)
- Python 爬虫使用动态切换ip防止封杀
- 【python爬虫】python使用代理爬虫例子