-
python post urllib_python爬虫urllib中的post如何实现?
2020-12-23 14:42:10开始小编也不清楚要教大家如何去实现,直到有小伙伴说希望去了解urllib的请求操作,那就针对这个内容,给大家讲述下实现方式吧~一、urlliburllib作为Python的标准库,基本上涵盖了基础的网络请求功能。二、发起POST...我们在进行爬虫的时候,首先,最重要的也是请求问题,只有先做好请求,才可以进行之后的一系列操作,因此,要怎么去写这个内容呢?开始小编也不清楚要教大家如何去实现,直到有小伙伴说希望去了解urllib的请求操作,那就针对这个内容,给大家讲述下实现方式吧~
一、urllib
urllib作为Python的标准库,基本上涵盖了基础的网络请求功能。
二、发起POST请求
urlopen()默认的访问方式是GET,当在urlopen()方法中传入data参数时,则会发起POST请求。注意:传递的data数据需要为bytes格式。timeout参数还可以设置超时时间,如果请求时间超出,那么就会抛出异常。from urllib import request
resp = request.urlopen('http://httpbin.org/post', data=b'word=hello', timeout=10)
print(resp.read().decode())
1234
三、使用带参数的POST方法:>>> import urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
>>> print f.read()
1234
以上就是关于post请求问题了,一共就几个步骤,大家可以直接把代码拿去跑程序试试呢~看下是不是满足大家的需求哦~如果还想查询其他内容,可以到云海天教程网了解下哈~
-
Python post请求 import urllib urllib2模块
2015-08-05 09:39:27Python post请求 import urllib urllib2模块本例中所用数据封装格式为{“json”:json数据格式},即
{"json":{"MSG":10000, "name" : "2015", "password": "123456"}}
开发环境 win7+Eclipse+Pydev,代码如下:
import urllib import urllib2 class Interface_post(): def __init__ (self, url, values, interfaceName): self.url = url self.values = values self.interfaceName = interfaceName def _post(self): json_val = {"json":self.values} data = urllib.urlencode(json_val) #对数据url编码 try: req = urllib2.Request(self.url) #post请求 response = urllib2.urlopen(req, data) re = response.read() print "接口名字: ", self.interfaceName print "服务器响应代号: ", response.getcode() print "服务器返回值为: ",re except URLError, e: print e.reason def regist(): #regist = Interface_post(url,values,interfaceName) #实例化对象 regist = Interface_post( 'http://10.10.10.10:8080/working/regist', #网址 { "MSG":10000, "name" : "2015", "password": "123456" }, #数据 "接口编号1,regist" #其他参数 ,用时调用 self.interfacaName ) return regist._post() #调用_post方法 def login():#..略 try: print "统计数据中---\n" regist() login() #调用函数发送请求,响应慢时建议对多个接口使用多线程控制 except Exception, e: print e.reason
Python中文参数传递出错的一种解决办法:详见http://blog.csdn.net/y396397735/article/details/47421719
-
python3 urllib post_Python 爬虫 urllib模块:post方式
2020-12-04 00:46:58本程序以爬取 'http://httpbin.org/post'为例格式:导入urllib.request导入urllib.parse数据编码处理,再设为utf-8编码: bytes(urllib.parse.urlencode({'word': 'hello'}), encoding = 'utf-8')打开爬取的网页: ...本程序以爬取 'http://httpbin.org/post' 为例
格式:
导入urllib.request
导入urllib.parse
数据编码处理,再设为utf-8编码: bytes(urllib.parse.urlencode({'word': 'hello'}), encoding = 'utf-8')
打开爬取的网页: response = urllib.request.urlopen('网址', data = data)
读取网页代码: html = response.read()
打印:
1.不decode
print(html) #爬取的网页代码会不分行,没有空格显示,很难看
2.decode
print(html.decode()) #爬取的网页代码会分行,像写规范的代码一样,看起来很舒服
查询请求结果:
a. response.status # 返回 200:请求成功 404:网页找不到,请求失败
b. response.getcode() # 返回 200:请求成功 404:网页找不到,请求失败
1.不decode的程序如下:import urllib.request
import urllib.parsse
data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding = 'utf-8')
response = urllib.request.urlopen(' data = data )
html = response.read()
print(html)
print("------------------------------------------------------------------")
print("------------------------------------------------------------------")
print(response.status)
print(response.getcode())
运行结果:
2.带decode的程序如下:import urllib.request
import urllib.parsse
data = bytes(urllib.parse.urlencode({'word': 'hello'}), encoding = 'utf-8')
response = urllib.request.urlopen(' data = data )
html = response.read()
print(html.decode())
print("------------------------------------------------------------------")
print("------------------------------------------------------------------")
print(response.status)
print(response.getcode())
运行结果:{
"args": {},
"data": "",
"files": {},
"form": {
"word": "hello"
},
"headers": {
"Accept-Encoding": "identity",
"Connection": "close",
"Content-Length": "10",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Python-urllib/3.4"
},
"json": null,
"origin": "106.14.17.222",
"url": "http://httpbin.org/post"
}
------------------------------------------------------------------
------------------------------------------------------------------
200
200
为什么要用bytes转换?
因为data = urllib.parse.urlencode({'word': 'hello'}) ##没有用bytes
response = urllib.request.urlopen('http://httpbin.org/post', data = data )
html = response.read()
错误提示:Traceback (most recent call last):
File "/usercode/file.py", line 15, in
response = urllib.request.urlopen('http://httpbin.org/post', data = data )
File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.4/urllib/request.py", line 453, in open
req = meth(req)
File "/usr/lib/python3.4/urllib/request.py", line 1104, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.
由此可见,post方式需要将请求内容用二进制编码。
classbytes([source[, encoding[, errors]]])
Return a new “bytes” object, which is an immutable sequence of integers in the range 0 <= x
Accordingly, constructor arguments are interpreted as for bytearray().
-
urllib-post
2018-11-11 09:46:04import urllib.request import urllib.parse cname = input('请输入要...post_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname' data = { 'cname': cname, 'pid': '', 'pageIndex': '1...import urllib.request import urllib.parse cname = input('请输入要查询的城市:') post_url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname' data = { 'cname': cname, 'pid': '', 'pageIndex': '1', 'pageSize': '10', } data = urllib.parse.urlencode(data).encode('utf8') headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36', } request = urllib.request.Request(post_url, headers=headers) response = urllib.request.urlopen(request, data=data) print(response.read().decode('utf8'))
-
python urllib post_python爬虫第1章 urllib库(二) urllib发送post请求
2021-03-17 02:33:39python爬虫第1章 urllib库(二) urllib发送post请求一、构建请求对象Request先看看python urllib默认的请求头:import urllib.requesturl = r"http://www.baidu.com"response = urllib.request.urlopen(url)print... -
python urllib2 post请求_python通过urllib2提交http post请求
2021-01-14 06:33:07python通过urllib2提交.../usr/bin/python#coding=utf-8import urllibimport urllib2def post(url, data):req = urllib2.Request(url)data = urllib.urlencode(data)#enable cookieopener = urllib2.build_ope... -
python urllib2 post_python利用urllib和urllib2访问http的GET/POST详解
2020-12-22 13:59:06前言本文主要给大家介绍了关于python如何访问http的GET/POST的相关内容,使用urllib和urllib2,可以轻松实现对http的访问,下面话不多说了,来一起看看详细的介绍吧。示例详解以下给个例子,实现对... -
python urllib2 post请求_python使用urllib2提交http post请求
2021-01-14 06:33:06/usr/bin/python#coding=utf-8import urllibimport urllib2def post(url, data):req = urllib2.Request(url)data = urllib.urlencode(data)#enable cookieopener = urllib2.build_opener(urllib2.HTTPCookieProces..... -
python爬虫urllib的post_python爬虫urllib中的post怎样实现
2021-03-06 16:34:37python爬虫urllib中的post怎样实现发布时间:2020-11-11 09:27:15来源:亿速云阅读:76作者:小新小编给大家分享一下python爬虫urllib中的post怎样实现,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考... -
python urllib2 post_python使用urllib2提交httppost请求的方法
2020-12-22 13:59:03本文实例讲述了python使用urllib2提交http post请求的方法。.../usr/bin/python#coding=utf-8import urllibimport urllib2def post(url, data):req = urllib2.Request(url)data = urllib.urlencode(data)#enable c... -
python urllib2 post_python使用urllib2提交http post请求的方法
2020-12-22 13:59:06本文实例讲述了python使用urllib2提交http post请求的方法。.../usr/bin/python#coding=utf-8import urllibimport urllib2def post(url, data):req = urllib2.Request(url)data = urllib.urlencode(data)#enable c... -
python urllib post请求_python使用urllib2提交http post请求的方法
2021-01-28 19:23:56本文实例讲述了python使用urllib2提交http post请求的方法。.../usr/bin/python#coding=utf-8import urllibimport urllib2def post(url, data):req = urllib2.Request(url)data = urllib.urlencode(data)#enable ... -
python urllib post_python学习之利用urllib和urllib2访问http的GET/POST详解
2020-12-04 10:09:00前言本文主要给大家介绍了关于python如何学习访问http的GET/POST的相关内容,使用urllib和urllib2,可以轻松实现对http的访问,下面话不多说了,来一起看看详细的介绍吧。示例详解以下给个例子,实现对... -
python爬虫urllib的post_python爬虫urllib中的post如何实现?
2020-12-04 10:13:07开始小编也不清楚要教大家如何去实现,直到有小伙伴说希望去了解urllib的请求操作,那就针对这个内容,给大家讲述下实现方式吧~一、urlliburllib作为Python的标准库,基本上涵盖了基础的网络请求功能。二、发起POST... -
python使用urllib发送post请求_使用urllib发出post请求
2021-01-14 04:29:53我正在尝试对API提供程序提出请求curl ... -
python爬虫urllib的post_python爬虫 urllib模块发起post请求过程解析
2020-12-04 10:13:06urllib模块发起的POST请求案例:爬取百度翻译的翻译结果1.通过浏览器捉包工具,找到POST请求的url针对ajax页面请求的所对应url获取,需要用到浏览器的捉包工具。查看百度翻译针对某个字条发送ajax请求,所对应的url... -
python利用urllib和urllib2访问http的GET/POST详解
2020-12-26 11:44:00本文主要给大家介绍了关于python如何访问http的GET/POST的相关内容,使用urllib和urllib2,可以轻松实现对http的访问,下面话不多说了,来一起看看详细的介绍吧。 示例详解 以下给个例子,实现对... -
python urllib2 post请求_使用python urllib2发送POST请求并获取响应
2021-01-14 06:33:06我正在尝试从发送POST请求中获取HTML页面:import httplibimport urllibimport urllib2from BeautifulSoup import BeautifulSoupheaders = {'Host': 'digitalvita.pitt.edu','Connection': 'keep-alive','Content-... -
python使用urllib发送post请求_使用urllib发送post请求
2021-01-14 04:29:53#导入httplib import httplib #连接服务器 conn=httplib.HTTPConnection('www.python.or... 文章 技术小美 2017-11-10 830浏览量 python爬虫URL编码和GETPOST请求 | python爬虫实战之三 urllib.parse模块 该模块... -
urllib
2018-12-02 21:44:30post 一、如同postman的数据在请求体中,并为json数据,并不在URL中 import urllib import json from urllib.request import urlopen from urllib.request import Request from urllib.parse import urlencode ... -
Python3 urllib Post请求
2019-06-28 11:57:35Python3 urllib Post请求 自己练手的一个小项目,使用Python3中自带的网络库urllib,发送post请求,请求参数为json字符串。 url = 'http://xxxx.com' params = { a:'1', b:'2' } params = json.dumps(params) ... -
python urllib发送post请求_python爬虫 urllib模块发起post请求过程解析
2021-03-05 13:34:11urllib模块发起的POST请求案例:爬取百度翻译的翻译结果1.通过浏览器捉包工具,找到POST请求的url针对ajax页面请求的所对应url获取,需要用到浏览器的捉包工具。查看百度翻译针对某个字条发送ajax请求,所对应的url... -
python urllib post请求_python爬虫(五)_urllib2:Get请求和Post请求
2020-12-03 05:30:57本篇将介绍urllib2的Get和Post方法,更多内容请参考:python学习指南urllib2默认只支持HTTP/HTTPS的GET和POST方法urllib.urlencode()urllib和urllib2都是接受URL请求的相关参数,但是提供了不同的功能。两个最显著的... -
urllib2提交http post请求
2019-10-06 00:33:56urllib2提交http post请求 urllib2提交http post请求 - keep it simple, stupid - ITeye技术网站urllib2提交http post请求 博客分类:PythonPythonCC++C#urllib2实现是... -
python urllib post_Python3.6通过自带的urllib通过get或post方法请求url
2021-01-28 19:23:51Python3.6通过自带的urllib通过get或post方法请求url# coding:utf-8from urllib import requestfrom urllib import parseurl = "http://10.1.2.151/ctower-mall-c/sys/login/login.do"data = {"id":"wdb","pwd":"wdb...