当前位置:网站首页>urllib urllib2
urllib urllib2
2022-06-29 13:46:00 【51CTO】
#-*-coding:utf-8-*-
import urllib import urllib2 import cookielib ##urllib url="http://www.qq.com" header={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"}
def urllib_study(url):
urldata=urllib.urlopen(url) print(urldata.read().decode('gbk'))## Web content print(urldata.getcode())##http Status code print(urldata.geturl())## request url print(urldata.info()) ##header info urldata.info().getparam('charset') obtain header code urllib.urlretrieve(url,'D:\\pic\\')## Download the file def urlretrieve_study(url,savepath): def callback(a,b,c): down_progess=100.0*a*b/c if down_progess>100: down_progess=100 print '%.2f%%'%down_progess urllib.urlretrieve(url,savepath,callback)
#urlretrieve_study(url,'D:\\pics\\qq.html')
##urllib2 def urllib2_study(url): urldata=urllib2.urlopen(url)
#-*-coding:utf-8-*-
import urllib2
import random
url=""
useragent=["Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
]
def get_content(url,useragent):
"""
@
"""
random_header=random.choice(useragent)
req=urllib2.Request(url)
req.add_header('User-Agent',random_header)
req.add_header('Host',)
req.add_header("Refer",)
req.add_header('GET',)
ret=urllib2.urlopen(req).read()
return ret
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
print(dir(urldata))##urldata.read.decode('gbk') urldata.getcode() urldata.info() urldata.geturl() cookie=cookielib.CookieJar() opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) data=opener.open(url)##urllib2.urlopen In fact, it is also called opener.open print(data.read().decode('gbk')) def urllib2_post(url): values={'name':'howhy','age':32} data=urllib.urlencode(values) req=urllib2.Request(url,data,header) response=urllib2.urlopen(req) the_page=response.read()
def handler():## commonly handler
htp=urllib2.HTTPHandler()
opener=urllib2.build_opener(htp)
req=urllib2.Request("http://www.baidu.com/")
print(opener.open(req).read())
def proxy():## agent handler
hand=urllib2.ProxyHandler({"http":"219.141.153.41:80"})
opener=urllib2.build_opener(hand)
req=urllib2.Request("http://www.baidu.com/")
print(opener.open(req).read())
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
def authhandler():## Basic authentication handler
htppwd=urllib2.HTTPPasswordMgrWithDefaultRealm()
htppwd.add_password(None,"auth web","username","password")
htp=urllib2.HTTPBasicAuthHandler(htppwd)
opener=urllib2.build_opener(htp) ## You can add more handler
opener.addheaders=[("User-Agent","dsffsdfdsfd"),()]
req=urllib2.Request("http://auth web")
print(opener.open(req).read())
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
边栏推荐
- 【无标题】安装依赖报错:Refusing to install package with name “***“ under a package
- MySQL常用语句和命令汇总
- The former security director of Uber faced fraud allegations and concealed the data leakage event
- 基于51单片机控制的BUCK开关电源Proteus仿真
- Openssl证书工具使用手册
- C language simulation to realize all character functions
- win32版俄罗斯方块(学习MFC必不可少)
- Pod security policy (PSP)
- 韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
- 【云驻共创】通过Rust语言计算加速技术突破图片识别性能瓶颈
猜你喜欢

DeeCamp2022正式开营!李开复、张亚勤亲授大师课 | 创新事

Lecun published a research plan for the next decade: AI autonomous intelligence with a 62 page paper

基于51单片机控制的BUCK开关电源Proteus仿真

MySQL tuning

Why is the integration of storage and computing a new direction of core making Collision school x Zhicun Technology

CVPR上百人中招新冠,emoji成“呈堂证供”,M2 MBP被曝硬盘降速,今日更多大新闻在此...

Weserver publishing map service

Ordinary users use vscode to log in to SSH and edit the root file

C语言内存函数

The imshow function of Matplotlib displays grayscale images. Vmin and vmax2 parameters should be set
随机推荐
运动App如何实现端侧后台保活,让运动记录更完整?
Solution to inconsistency between RTC time and world time on the server
Appkey when applying for offline packaging of uniapp
想做个答题类的微信小游戏?读这篇文章就够了
自主可控再下一城!首套国产ARTIQ架构量子计算测控系统发布
【毕业季】这四年一路走来都很值得——老学长の忠告
Acwing game 57
Windbg常用命令详解
GEE——美国LANDFIRE火灾数据集
编写一个shell脚本,求一个数的”逆序数“
The former security director of Uber faced fraud allegations and concealed the data leakage event
mysql调优
Getting started with mybaits (including example tutorial and source code)
C language__ VA_ ARGS__ Usage of
Cisco simulator simple campus network design, final assignment difficulty
How to set the safety line and safety margin for futures trading?
Openssl证书工具使用手册
B+树|MYSQL索引使用原则
C language character function
C语言的指针详解