python爬虫之urllib
#coding=utf-8
#urllib操作类 import time
import urllib.request
import urllib.parse
from urllib.error import HTTPError, URLError
import sys
class myUrllib: @staticmethod
def get_headers(headers):
default_headers = {
'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
#'Referer': r'http://www.baidu.com/',
'Connection': 'keep-alive',
'Cookie':'uuid_tt_dd=2845574184150781887; _ga=GA1.2.1608505838; dc_tos=p308'
}
headers = headers and dict(default_headers,**headers) or default_headers
return headers @staticmethod
def get(url,headers={}):
headers = myUrllib.get_headers(headers)
#data=urllib.parse.urlencode(query_data).encode('utf-8')
#r/R:非转义的原始字符串
#u/U:表示unicode字符串
#b:bytes
url=r'%s'%url
request = urllib.request.Request(url,headers=headers,method='GET')
try:
html = urllib.request.urlopen(request).read()
page = html.decode('utf-8')
except HTTPError as e:
print (e.code,e.reason)
except URLError as e:
print (e.reason)
return page @staticmethod
def post(url,data={},headers={}):
headers = myUrllib.get_headers(headers)
data=urllib.parse.urlencode(data)
binary_data=data.encode('utf-8')
url=r'%s'%url
request=urllib.request.Request(url,data=binary_data,headers=headers,method='POST')#发送请求,传送表单数据
# response=urllib.request.urlopen(request)#接受反馈的信息
# data=response.read()#读取反馈信息
# data=data.decode('utf-8')
#print (data.encode('gb18030'))
#print (response.geturl())#返回获取的真实的URL
#info():返回一个对象,表示远程服务器返回的头信息。
#getcode():返回Http状态码,如果是http请求,200表示请求成功完成;404表示网址未找到。
#geturl():返回请求的url地址。 try:
html = urllib.request.urlopen(request).read()
page = html.decode('utf-8')
except HTTPError as e:
print (e.code,e.reason)
except URLError as e:
print (e.reason)
return page getInfo = myUrllib.get('http://localhost:88/test/c.php?act=category',{'Referer': r'https://www.baidu.com/'})
print(getInfo) sys.exit() postInfo = myUrllib.post('http://localhost:88/test/c.php',{'id':1010},{'Referer': r'https://www.baidu.com/'})
print(postInfo)
d:\python\crawler>python urllib01.py
HTTP_HOST:
localhost:88
HTTP_USER_AGENT:
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/63.0.3239.108 Safari/537.36
HTTP_COOKIE:
uuid_tt_dd=2845574184150781887; _ga=GA1.2.1608505838; dc_tos=p308
HTTP_REFERER:
https://www.baidu.com/
REQUEST_METHOD:
GET
GET DATA:
array(1) {
["act"]=>
string(8) "category"
}
#设置代理
#coding=utf-8
import urllib.request
import random
from urllib.error import HTTPError, URLError def proxy_handler(url,iplist,wfile):
#ip = random.choice(iplist)
for ip in iplist:
try:
print('*'*20,'\n ip:',ip)
proxy_support = urllib.request.ProxyHandler({'http':ip})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-Agent',r'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36')]
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
code = response.getcode()
url = response.geturl()
print('*'*20,'\n url:',url)
print('*'*20,'\n code:',code)
info = response.info()
print('*'*20,'\n info:',info)
if code == 200:
page = response.read()
#写入文件
page = str(page, encoding='utf-8')
fw = open(wfile,'w',encoding='UTF-8')
fw.write(page)
fw.close()
print('*'*20,'\n write file:',wfile)
break
except HTTPError as e:
print (e.code,e.reason)
continue
except URLError as e:
print (e.reason)
continue url = r'http://ip.chinaz.com/'
iplist = ['182.42.244.169:808','122.72.18.34:80','52.44.16.168:3129']
wfile = 'page.txt'
proxy_handler(url,iplist,wfile)
d:\python\crawler>python proxy01.py
********************
ip: 182.42.244.169:808
[WinError 10061] 由于目标计算机积极拒绝,无法连接。
********************
ip: 122.72.18.34:80
********************
url: http://ip.chinaz.com/
********************
code: 200
********************
info: Cache-Control: private
Content-Length: 33900
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Set-Cookie: qHistory=aHR0cDovL2lwLmNoaW5hei5jb20rSVAv5pyN5Yqh5Zmo5Zyw5Z2A5p+l6K
i; domain=.chinaz.com; expires=Tue, 05-Feb-2019 15:03:42 GMT; path=/
X-Powered-By: ASP.NET
Date: Mon, 05 Feb 2018 15:03:42 GMT
X-Cache: MISS from GD-SZ-WEB-01
X-Cache-Lookup: MISS from GD-SZ-WEB-01:80
Connection: close
********************
write file: page.txt
python爬虫之urllib的更多相关文章
- Python爬虫之urllib模块2
Python爬虫之urllib模块2 本文来自网友投稿 作者:PG-55,一个待毕业待就业的二流大学生. 看了一下上一节的反馈,有些同学认为这个没什么意义,也有的同学觉得太简单,关于Beautiful ...
- Python爬虫之urllib模块1
Python爬虫之urllib模块1 本文来自网友投稿.作者PG,一个待毕业待就业二流大学生.玄魂工作室未对该文章内容做任何改变. 因为本人一直对推理悬疑比较感兴趣,所以这次爬取的网站也是平时看一些悬 ...
- python爬虫之urllib库(三)
python爬虫之urllib库(三) urllib库 访问网页都是通过HTTP协议进行的,而HTTP协议是一种无状态的协议,即记不住来者何人.举个栗子,天猫上买东西,需要先登录天猫账号进入主页,再去 ...
- python爬虫之urllib库(二)
python爬虫之urllib库(二) urllib库 超时设置 网页长时间无法响应的,系统会判断网页超时,无法打开网页.对于爬虫而言,我们作为网页的访问者,不能一直等着服务器给我们返回错误信息,耗费 ...
- python爬虫之urllib库(一)
python爬虫之urllib库(一) urllib库 urllib库是python提供的一种用于操作URL的模块,python2中是urllib和urllib2两个库文件,python3中整合在了u ...
- Python爬虫之urllib.parse详解
Python爬虫之urllib.parse 转载地址 Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url 解析url( urlparse() ) ur ...
- Python爬虫之Urllib库的基本使用
# get请求 import urllib.request response = urllib.request.urlopen("http://www.baidu.com") pr ...
- python爬虫之urllib库
请求库 urllib urllib主要分为几个部分 urllib.request 发送请求urllib.error 处理请求过程中出现的异常urllib.parse 处理urlurllib.robot ...
- Python爬虫系列-Urllib库详解
Urllib库详解 Python内置的Http请求库: * urllib.request 请求模块 * urllib.error 异常处理模块 * urllib.parse url解析模块 * url ...
- python爬虫之urllib库介绍
一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...
随机推荐
- PostgreSQL 自定义自动类型转换(CAST)
转载自:https://yq.aliyun.com/articles/228271 背景 PostgreSQL是一个强类型数据库,因此你输入的变量.常量是什么类型,是强绑定的,例如 在调用操作符时,需 ...
- android 监控应用进程
在android系统中,怎么监控应用的进程改变及消亡呢? 至于监控应用进程能做什么,这个就不多说了,你懂的. 在android系统中有这么一个类ActivityManagerNative,看名称就大概 ...
- Kafka 配置安装
1.从官网下载安装包 http://kafka.apache.org/downloads2.上传到01虚拟机,解压3.进入安装目录下的config目录4.对server.properties进行配置 ...
- Dapper/SqlMapper映射对应问题
Dapper, a light weight object mapper for ADO.NET 轻量级orm辅助类,不用配置,直接引入一个cs文件或者引入其dll就能使用. 使用连接对象的query ...
- wordpress 插件Simple Social Buttons import处漏洞复现
前言: 漏洞范围范围:simple socail buttons v2.0.4到v2.0.22之间的所有版本 利用条件,wordpress的普通用户 漏洞细节:该插件缺少权限的检查,非管理管权限执行管 ...
- python网页爬虫开发之五-反爬
1.头信息检查是否频繁相同 随机产生一个headers, #user_agent 集合 user_agent_list = [ 'Mozilla/5.0 (Windows NT 6.1; WOW64 ...
- Java笔试面试题整理第四波
转载至:http://blog.csdn.net/shakespeare001/article/details/51274685 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- python-web自动化-三种等待方式
当有元素定位不到时,比如下拉框,弹出框等各种定位不到时:一般是两种问题:1 .有frame :2.没有加等待 下面学习三种等待方式: 1.强制等待 sleep(xx)这种方法简单粗暴,不管浏览器是否加 ...
- 性能监控扩展篇(grafana + influxdb + telegraf)
之前已经说过了自己写sh脚本监控,我看有人评论了说用telegraf进行数据收集,于是乎去研究了下,感觉还可以,不过磁盘io的的表个人感觉有些美中不足,并未直接给出读写速率的情况,可能是研究时间太短, ...
- 插件: Hammer.js
官网: http://hammerjs.github.io/ hammer.js 官网 http://hammerjs.github.io/api/ 官网API(官网写的实在太简了!不好用.注意里面 ...