当前位置:网站首页>urllib urllib2
urllib urllib2
2022-06-29 12:39: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'))##网页内容 print(urldata.getcode())##http状态码 print(urldata.geturl())## request url print(urldata.info()) ##header info urldata.info().getparam('charset')获取header 编码 urllib.urlretrieve(url,'D:\\pic\\')##下载文件 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其实也是调用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():##一般handler
htp=urllib2.HTTPHandler()
opener=urllib2.build_opener(htp)
req=urllib2.Request("http://www.baidu.com/")
print(opener.open(req).read())
def proxy():##代理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():##基本认证handler
htppwd=urllib2.HTTPPasswordMgrWithDefaultRealm()
htppwd.add_password(None,"auth web","username","password")
htp=urllib2.HTTPBasicAuthHandler(htppwd)
opener=urllib2.build_opener(htp) ##此时可以加多个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.
边栏推荐
- Memorized Function
- Hutool tool class learning (continuous update)
- AOSP ~ logcat persistence
- leetcode 522. Longest special sequence II
- YOLO系列梳理(九)初尝新鲜出炉的YOLOv6
- Clickhouse database uses JDBC to store milliseconds and nanoseconds
- Learn from the official motor and BLDC control strategies, and make money as soon as you learn
- Memorized Function
- *Clock in algorithm *leetcode 146 Analysis of LRU cache algorithm
- Tree array application (acwing 24224244)
猜你喜欢

weserver發布地圖服務

matplotlib的imshow函数显示灰度图像要设置vmin和vmax2个参数

【系统设计】邻近服务

Cvpr2022 𞓜 thin domain adaptation

Yyds dry inventory solution sword finger offer: find the nearest common ancestor of two nodes in the binary tree

Qitai observation: professional elites must step on the huge pit of entrepreneurship - learning effect pit

别再重复造轮子了,推荐使用 Google Guava 开源工具类库,真心强大!

Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions

CVPR2022 | 重新审视池化:你的感受野不是最理想的

Cvpr2022 | panopticdepth: a unified framework for depth aware panoramic segmentation
随机推荐
C language character function
Tree array application (acwing 24224244)
Create an API rapid development platform, awesome!
从零搭建Pytorch模型教程(四)编写训练过程--参数解析
Weserver publishing map service
Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
Detailed explanation of C language pointer
PG基础篇--逻辑结构管理(1)
Lecun published a research plan for the next decade: AI autonomous intelligence with a 62 page paper
Netdata mail alarm configuration
System. Currenttimemillis() and system Nanotime() which is faster? Most people get the wrong answer!
维修记录导出的excel表格太大怎么办?
The node command in the script does not print the execution log on the console
YOLO系列梳理(九)初尝新鲜出炉的YOLOv6
CVPR 2022 | unknown target detection module study: learning unknown targets in video
Check yaml file security configuration: kubesec
【云驻共创】通过Rust语言计算加速技术突破图片识别性能瓶颈
极光 · 哈夫曼树の生成(线段树结构 非指针)(仿邻接表)
*打卡算法*LeetCode 146. LRU 缓存 算法解析
Uncover the secret! Pay attention to those machines under the membership system!