Python实现爬虫设置代理IP和伪装成浏览器的方法分享

yipeiwu_com5年前Python爬虫

1.python爬虫浏览器伪装

#导入urllib.request模块
import urllib.request
#设置请求头
headers=("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
#创建一个opener
opener=urllib.request.build_opener()
#将headers添加到opener中
opener.addheaders=[headers]
#将opener安装为全局
urllib.request.install_opener(opener)
#用urlopen打开网页
data=urllib.request.urlopen(url).read().decode('utf-8','ignore')

2.设置代理

#定义代理ip
proxy_addr="122.241.72.191:808"
#设置代理
proxy=urllib.request.ProxyHandle({'http':proxy_addr})
#创建一个opener
opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle)
#将opener安装为全局
urllib.request.install_opener(opener)
#用urlopen打开网页
data=urllib.request.urlopen(url).read().decode('utf-8','ignore')

3.同时设置用代理和模拟浏览器访问

#定义代理ip
proxy_addr="122.241.72.191:808"
#创建一个请求
req=urllib.request.Request(url)
#添加headers
req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
#设置代理
proxy=urllib.request.ProxyHandle("http":proxy_addr)
#创建一个opener
opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle)
#将opener安装为全局
urllib.request.install_opener(opener)
#用urlopen打开网页
data=urllib.request.urlopen(req).read().decode('utf-8','ignore')

4.在请求头中添加多个信息

import urllib.request
page_headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0",
       "Host":"www.baidu.com",
       "Cookie":"xxxxxxxx"
       }
req=urllib.request.Request(url,headers=page_headers)
data=urllib.request.urlopen(req).read().decode('utf-8','ignore')

5.添加post请求参数

import urllib.request
import urllib.parse
#设置post参数
page_data=urllib.parse.urlencode([
                  ('pn',page_num),
                  ('kd',keywords)
                  ])
#设置headers
page_headers={
     'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0',
     'Connection':'keep-alive',
     'Host':'www.lagou.com',
     'Origin':'https://www.lagou.com',
     'Cookie':'JSESSIONID=ABAAABAABEEAAJA8F28C00A88DC4D771796BB5C6FFA2DDA; user_trace_token=20170715131136-d58c1f22f6434e9992fc0b35819a572b',
     'Accept':'application/json, text/javascript, */*; q=0.01',
     'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
     'Referer':'https://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98?labelWords=&fromSearch=true&suginput=',
     'X-Anit-Forge-Token':'None',
     'X-Requested-With':'XMLHttpRequest'
     }
#打开网页
req=urllib.request.Request(url,headers=page_headers)
data=urllib.request.urlopen(req,data=page_data.encode('utf-8')).read().decode('utf-8')

6.利用phantomjs模拟浏览器请求

#1.下载phantomjs安装到本地,并设置环境变量
from selenium import webdriver
bs=webdriver.PhantomJS()
#打开url
bs.get(url)
#获取网页源码
url_data=bs.page_source
#将浏览到的网页保存为图片
bs.get_screenshot_as_file(filename)

7.phantomjs设置user-agent和cookie

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0")
bs = webdriver.PhantomJS(desired_capabilities=dcap)
bs.get(url)
#删除cookie
bs.delete_all_cookies()
#设置cookie
#cookie格式:在浏览器cookie中查看,一个cookie需要包含以下参数,domain、name、value、path
cookie={
  'domain':'.www.baidu.com', #注意前面有.
  'name':'xxxx', 
  'value':'xxxx', 
  'path':'xxxx'
  }
#向phantomjs中添加cookie
bs.add_cookie(cookie)

8.利用web_driver工具

#1.下载web_driver工具(如chromdriver.exe)及对应的浏览器
#2.将chromdriver.exe放到某个目录,如c:\chromdriver.exe
from selenium import webdriver
driver=webdriver.Chrome(executable_path="C:\chromdriver.exe")
#打开url
driver.get(url)

以上这篇Python实现爬虫设置代理IP和伪装成浏览器的方法分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 中xpath爬虫实例详解

python 中xpath爬虫实例详解

案例一: 某套图网站,套图以封面形式展现在页面,需要依次点击套图,点击广告盘链接,最后到达百度网盘展示页面。 这一过程通过爬虫来实现,收集百度网盘地址和提取码,采用xpath爬虫技术...

Python爬虫爬取Bilibili弹幕过程解析

Python爬虫爬取Bilibili弹幕过程解析

先来思考一个问题,B站一个视频的弹幕最多会有多少? 比较多的会有2000条吧,这么多数据,B站肯定是不会直接把弹幕和这个视频绑在一起的。 也就是说,有一个视频地址为https://www...

Python爬虫之网页图片抓取的方法

Python爬虫之网页图片抓取的方法

一、引入 这段时间一直在学习Python的东西,以前就听说Python爬虫多厉害,正好现在学到这里,跟着小甲鱼的Python视频写了一个爬虫程序,能实现简单的网页图片下载。 二、代码...

使用Python3编写抓取网页和只抓网页图片的脚本

最基本的抓取网页内容的代码实现: #!/usr/bin/env python from urllib import urlretrieve def firstNonBl...

Python爬虫动态ip代理防止被封的方法

Python爬虫动态ip代理防止被封的方法

在爬取的过程中难免发生ip被封和403错误等等,这都是网站检测出你是爬虫而进行反爬措施,在这里为大家总结一下怎么用IP代理防止被封 首先,设置等待时间: 常见的设置等待时间有两种,一种是...