ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2021-10-15

2021-10-15 11:31:27  阅读:172  来源: 互联网

标签:10 15 url res urllib3 send urlstr httpMgr 2021


import urllib3
import json

def write_to_file(filename, html):
f=open(filename,‘w’,encoding=‘utf-8’);
f.write(html);
f.close;

def load_page(filename,res):
html = res.data.decode(‘utf-8’)
print(html)
write_to_file(filename, html)

def sent_url():
urlstr=“http://www.baidu.com”

httpMgr=urllib3.PoolManager();
res=httpMgr.request("GET", urlstr)

print("status:%d" % res.status)
# print(res.data)
load_page('send_url.html', res)

def send_url_with_headers():
urlstr = “http://www.baidu.com”
headers={
“x-something”:“value”
}
httpMgr = urllib3.PoolManager();
res=httpMgr.request(“GET”, urlstr, headers=headers)
print(“status:%d” % res.status)
# print(res.data)
load_page(‘send_url_with_headers.html’, res)

def send_get_with_param001():
urlstr=“http://httpbin.org/get”
param={
‘arg1’:‘value1’,
‘arg2’:‘value2’
}
httpMgr = urllib3.PoolManager();
res = httpMgr.request(“GET”, urlstr, fields=param)
load_page(“send_get_with_param001.html”, res)

def send_get_with_param002():
urlstr=“http://cn.bing.com/search”
word = {“q”:“Python网络爬虫”}
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36’
}
httpMgr = urllib3.PoolManager();
res = httpMgr.request(“GET”, urlstr, headers=headers, fields=word)
load_page(“send_get_with_param002.html”, res)

def send_post_with_field():
urlstr = “http://httpbin.org/post”
param = {
‘arg1’: ‘value1’,
‘arg2’: ‘value2’
}
httpMgr = urllib3.PoolManager();
res = httpMgr.request(“POST”, urlstr, fields=param)
print(“status:%d” % res.status)
# print(res.data)
load_page(‘send_post_with_field.html’, res)

def send_post_with_body():
urlstr = “http://httpbin.org/post”
body = {
‘arg1’: ‘value1’,
‘arg2’: ‘value2’
}
encode_data=json.dumps(body).encode(‘utf-8’)
httpMgr = urllib3.PoolManager();
res=httpMgr.request(“POST”,urlstr,body=encode_data)
load_page(‘send_post_with_body.html’,res)
def send_post_with_file():
with open(‘ex.txt’) as fp:
file_data=fp.read()

urlstr = "http://httpbin.org/post"
httpMgr = urllib3.PoolManager();
res = httpMgr.request("POST", urlstr,
                      fields={
                         'filefields':('ex.txt', file_data, 'text/plain')
                      })
load_page('send_post_with_file_html', res)

def send_url_with_proxy():
try:
urlstr = “http://httpbin.org/ip”
httpMgr = urllib3.PoolManager();
res001= httpMgr.request(“GET”, urlstr)
load_page(‘send_url_with_proxy1_html’, res001)
proxy_httpMgr=urllib3.ProxyManager(‘http://50.233.137.33:80’,
headers={
‘connection’:‘keep-alive’
})
res=proxy_httpMgr.request(“GET”,urlstr,timeout=4.0)
load_page(‘send_url_with_proxy2_html’, res)

except urllib3.exceptions.MaxRetryError as e:
    print(e)

def send_url_with_timeout001():
urlstr=“http://httpbin.org/delay/3”
httpMgr=urllib3.PoolManager()
res=httpMgr.request(“GET”,urlstr,timeout=4.0)
print(“status:%d” % res.status)
load_page(‘send_url_with_timeout001.html’,res)

def send_url_with_timeout002():
urlstr=“http://httpbin.org/delay/3”
httpMgr=urllib3.PoolManager()
res=httpMgr.request(“GET”,urlstr,timeout=urllib3.Timeout(connect=1.0,read=2.0))
print(“status:%d” % res.status)
load_page(‘send_url_with_timeout001.html’,res)

def send_url_with_timeout003():
try:
urlstr=“http://httpbin.org/delay/3”
httpMgr=urllib3.PoolManager(timeout=urllib3.Timeout(connect=1.0,read=2.0))
res=httpMgr.request(“GET”,urlstr)
print(“status:%d” % res.status)
load_page(‘send_url_with_timeout001.html’,res)

except urllib3.exceptions.ReadTimeoutError as e:
    print(e)
except urllib3.exceptions.MaxRetryError as e:
    print(e)

def send_url_with_exception():
try:
urlstr = “http://www.xiangbaoerzi.com”
httpMgr = urllib3.PoolManager(timeout=urllib3.Timeout(connect=1.0, read=4.0))
res = httpMgr.request(“GET”, urlstr,retries=False)
print(“status:%d” % res.status)
load_page(‘send_url_with_timeout001.html’, res)
except urllib3.exceptions.HTTPError as e:
print(e)
except urllib3.exceptions.MaxRetryError as e:
print(e)
except urllib3.exceptions.NewConnectionError as e:
print(e)
if name==‘main’:
# sent_url()
# send_url_with_headers()
# send_get_with_param001()
# send_get_with_param002()
# send_post_with_field()
# send_post_with_body()
send_post_with_file()
send_url_with_proxy()
send_url_with_timeout001()
send_get_with_param002()
send_url_with_timeout003()
send_url_with_exception()

标签:10,15,url,res,urllib3,send,urlstr,httpMgr,2021
来源: https://blog.csdn.net/weixin_51747549/article/details/120780067

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有