当前位置:首页 » 编程语言 » pythonurlrequest

pythonurlrequest

发布时间: 2023-07-09 12:04:03

‘壹’ python:Request的函数是什么作用

你说的是

1
“class Request( url[, data][, headers] [, origin_req_host][, unverifiable]) ”吧。
这汪中是一个类阿。是提取url中的信息的阿

“This class is an abstraction of a URL request.”
就像你在网络里面搜索“python”一样。
用户点完enter键触发。
这时候
URL = "http://www..com/s?wd=python"
Request(URL)
这样就生成了一个类册晌。你就可以用他来州陵锋解析用户需求。

2
request( method, url[, body[, headers]])

This will send a request to the server using the HTTP request method method and the selector url. If the body argument is present, it should be a string of data to send after the headers are finished. The header Content-Length is automatically set to the correct value. The headers argument should be a mapping of extra HTTP headers to send with the request.

‘贰’ 关于python里面的requests和urllib的request

前者是一个库,后者应该只是一个方法?前者比后者强大。

‘叁’ python通过get,post方式发送http请求和接收http响应


本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法。姿敏分享给轮敏大家供大家参考。
具体如下:
测试用CGI,名字为test.py,放在apache的cgi-bin目录下:
#!/usr/bin/python
import cgi
def main():
print Content-type: text/htmln
form = cgi.FieldStorage()
if form.has_key(ServiceCode) and form[ServiceCode].value != :
print h1 Hello,form[ServiceCode].value,/h1
else:
print h1 Error! Please enter first name./h1
main()
python发送post和get请求
get请求:
使用get方式时,请求数据直接放在url中。
方法一、
?
7
8
import urllib
import urllib2
url =
req = urllib2.Request(url)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
方法二、
?
7
import httplib
url =
conn = httplib.HTTPConnection(192.168.81.16)
conn.request(method=GET,url=url)
response = conn.getresponse()
res= response.read()
print res
post请求:腊册枝
使用post方式时,数据放在data或者body中,不能放在url中,放在url中将被忽略。
方法一、
import urllib
import urllib2
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
req = urllib2.Request(url = requrl,data =test_data_urlencode)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
方法二、
11
import urllib
import httplib
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
headerdata = {Host:192.168.81.16}
conn = httplib.HTTPConnection(192.168.81.16)
conn.request(method=POST,url=requrl,body=test_data_urlencode,headers = headerdata)
response = conn.getresponse()
res= response.read()
print res
对python中json的使用不清楚,所以临时使用了urllib.urlencode(test_data)方法;
模块urllib,urllib2,httplib的区别
httplib实现了http和https的客户端协议,但是在python中,模块urllib和urllib2对httplib进行了更上层的封装。
介绍下例子中用到的函数:
1、HTTPConnection函数
httplib.HTTPConnection(host[,port[,stict[,timeout]]])
这个是构造函数,表示一次与服务器之间的交互,即请求/响应
host 标识服务器主机(服务器IP或域名)
port 默认值是80
strict 模式是False,表示无法解析服务器返回的状态行时,是否抛出BadStatusLine异常
例如:
conn = httplib.HTTPConnection(192.168.81.16,80) 与服务器建立链接。
2、HTTPConnection.request(method,url[,body[,header]])函数
这个是向服务器发送请求
method 请求的方式,一般是post或者get,
例如:
method=POST或method=Get
url 请求的资源,请求的资源(页面或者CGI,我们这里是CGI)
例如:
url=
或者
url=
body 需要提交到服务器的数据,可以用json,也可以用上面的格式,json需要调用json模块
headers 请求的http头headerdata = {Host:192.168.81.16}
例如:
?
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
headerdata = {Host:192.168.81.16}
conn = httplib.HTTPConnection(192.168.81.16,80)
conn.request(method=POST,url=requrl,body=test_data_urlencode,headers = headerdata)
conn在使用完毕后,应该关闭,conn.close()
3、HTTPConnection.getresponse()函数
这个是获取http响应,返回的对象是HTTPResponse的实例。
4、HTTPResponse介绍:
HTTPResponse的属性如下:
read([amt]) 获取响应消息体,amt表示从响应流中读取指定字节的数据,没有指定时,将全部数据读出;
getheader(name[,default]) 获得响应的header,name是表示头域名,在没有头域名的时候,default用来指定返回值
getheaders() 以列表的形式获得header
例如:
?
1
2
3
4
5
date=response.getheader(date);
print date
resheader=
resheader=response.getheaders();
print resheader
列形式的响应头部信息:
?
1
2
3
[(content-length, 295), (accept-ranges, bytes), (server, Apache), (last-modified, Sat, 31 Mar 2012 10:07:02 GMT), (connection, close), (etag, e8744-127-4bc871e4fdd80), (date, Mon, 03 Sep 2012 10:01:47 GMT), (content-type, text/html)]
date=response.getheader(date);
print date
取出响应头部的date的值。
希望本文所述对大家的Python程序设计有所帮助。

‘肆’ python3爬虫urllib.request.urlopen("网址").read() 本来是utf-8,为什么还要加上urlencode(“utf-8”)

你这行代码是不需要urlencode()的。

对于返回的request对象,其read()方法获得的其实是一个字节流对象,而非字符串对象,所以这时需要调用该字节流对象的decode()方法,按指定编码方式进行解码。
至于urlencode(),这是urllib中的一个函数,它的作用是将字符串进行url编码。这个编码其实就是个转义的过程,将那些因可能造成解释器误会或安全问题而不适合出现在请求中的符号进行转义,并且把超出url编码表的字符降维。

‘伍’ 【Python爬虫】分析网页真实请求

1、抓取网页、分析请求
2、解析网页、寻找数据
3、储存数据、多页处理

翻页有规律:
很多网址在第一页时并没有变化,多翻下一页后规律就出来,比如 豆瓣第一页 和 豆瓣第三页

发现start为40,limit=20,所以猜测start=0就是第一页,每页显示20条数据,对于第三页显示的参数可以一个个删除验证,可以减去不必要的参数, 但是删除前一定要做好数据的对比

(1) 文本框输入后产生一个请求,如常见的登录、注册页面
Referer:表示当前请求的来源
Request URL:表示实际请求地址

翻页后URL不变,该如何寻找请求?
如: http://www.zkh360.com/zkh_catalog/3.html

通过对比可以发现网站是通过pageIndex参数控制翻页的,?表示连接

接下来用抓包工具分析下 ,从第四页开始看URL就知道了,但是前面几面需要查看请求的参数,这里偏多,就切换到【Inspectors--Webforms】选项,看的比较直观

类似的网站还有 今日头条 ,有兴趣的朋友可以去研究下
(可通过获取max_behot_time的值而改变as和cp)

‘陆’ python requests get方式怎么设置请求头

Header可以通过Request提供的.add_header()方法进行添加,示例代码如下:

  • 123456789101112#-*-coding:utf-8-*-

  • importurllib2importurlliburl='http://ah.example.com'half_url=u'/servlet/av/jd?

  • ai=782&ji=2624743&sn=I'#构造get请求req=urllib2.

  • Request(url+half_url.

  • encode('utf-8'))#添加headerreq.add_header('AcceptEncoding','gzip,deflate')req.

  • add_header('User-Agent','Mozilla/5.0')response=urllib2.

  • urlopen(req)

  • printresponse.

‘柒’ Python request 响应状态

我们可以检测响应状态码芦乱握:

r=requests.get(url)r.status_code

返回200,正常响应

为方便引用,Requests还附带了一个陪庆内置的状态码查询对象:

r = requests.get(url)

print r.status_code == requests.codes.ok

如果发送了一个失败请陪铅求(非200响应),我们可以通过Response.raise_for_status()来抛出异常:

bad_r = requests.get(url)

bad_r.raise_for_status()

热点内容
浙江电脑服务器租用虚拟主机 发布:2025-02-09 00:29:48 浏览:76
安卓网在哪里 发布:2025-02-09 00:29:36 浏览:391
汇编语言调用c语言 发布:2025-02-09 00:19:25 浏览:335
网络编程http 发布:2025-02-09 00:18:33 浏览:62
php的点餐系统 发布:2025-02-09 00:17:03 浏览:600
安卓区转苹果区会发生什么 发布:2025-02-09 00:16:26 浏览:131
c语言编译完怎么执行 发布:2025-02-09 00:16:16 浏览:27
AMD平台对应的C编译器 发布:2025-02-09 00:15:35 浏览:68
行李箱的密码锁哪里修 发布:2025-02-08 23:58:14 浏览:531
c语言字母ascii码表 发布:2025-02-08 23:55:49 浏览:838