美文网首页
python3-urllib.request操作

python3-urllib.request操作

作者: 锱三石五 | 来源:发表于2017-04-19 12:02 被阅读0次

请求网页

import urllib.request
f = urllib.request.urlopen('http:www.bing.com')
print(f.read().decode('utf-8'))

#获取状态码
f.getcode()
#获取url
f.geturl()
#获取meta-infomation,像headers
print(f.info())

发送数据请求

import urllib.request
 req = urllib.request.Request(url='https://localhost/cgi-bin/test.cgi',
                        data=b'This data is passed to stdin of the CGI')
f = urllib.request.urlopen(req)
print(f.read().decode('utf-8'))

put请求

import urllib.request
DATA=b'some data'
req = urllib.request.Request(url='http://localhost:8080', data=DATA,method='PUT')
f = urllib.request.urlopen(req)
print(f.status)
print(f.reason)

基本http验证,登录请求

import urllib.request
# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='PDQ Application',
                          uri='https://mahler:8092/site-updates.py',
                          user='klem',
                          passwd='kadidd!ehopper')
opener = urllib.request.build_opener(auth_handler)
# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)
urllib.request.urlopen('http://www.example.com/login.html')

支持代理方式验证请求

proxy_handler = urllib.request.ProxyHandler({'http': 'http://www.example.com:3128/'})
proxy_auth_handler = urllib.request.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')

opener = urllib.request.build_opener(proxy_handler, proxy_auth_handler)
# This time, rather than install the OpenerDirector, we use it directly:
opener.open('http://www.example.com/login.html')

添加headers

import urllib.request
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
opener.open('http://www.example.com/')
#或
import urllib.request
req = urllib.request.Request('http://www.example.com/')
req.add_header('Referer', 'http://www.python.org/')
# Customize the default User-Agent header value:
req.add_header('User-Agent', 'urllib-example/0.1 (Contact: . . .)')
r = urllib.request.urlopen(req)

带参数的post

import urllib.request
import urllib.parse
data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
data = data.encode('ascii')
with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f:
     print(f.read().decode('utf-8'))

带参数的get

import urllib.request
import urllib.parse
params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
url = "http://www.musi-cal.com/cgi-bin/query?%s" % params
with urllib.request.urlopen(url) as f:
     print(f.read().decode('utf-8'))

指定代理请求

import urllib.request
proxies = {'http': 'http://proxy.example.com:8080/'}
opener = urllib.request.FancyURLopener(proxies)
f = opener.open("http://www.python.org")
f.read().decode('utf-8')

无代理请求

import urllib.request
opener = urllib.request.FancyURLopener({})
with opener.open("http://www.python.org/") as f:
     f.read().decode('utf-8')

相关文章

  • python3-urllib.request操作

    请求网页 发送数据请求 put请求 基本http验证,登录请求 支持代理方式验证请求 添加headers 带参数的...

  • Mac 终端 Linux 简单命令

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

  • 常见Mac终端命令大全

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

  • Mac 终端命令大全

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

  • Mac 终端命令大全

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

  • Mac 终端常用命令汇总

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

  • shell 命令 干货

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

  • shell - 终端命令总结

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

  • MAC终端命令

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

  • Mac 终端命令大全

    目录操作 文件操作 选择操作 安全操作 编程操作 进程操作 时间操作 网络与通信操作 Korn Shell 命令 ...

网友评论

      本文标题:python3-urllib.request操作

      本文链接:https://www.haomeiwen.com/subject/ahmczttx.html