导航:首页 > 编程语言 > pythonrequests实例

pythonrequests实例

发布时间:2022-07-31 17:51:01

① [python] requests params

这是一个Python3读取页面的例子,仅供参考

importhttp.cookiejar
importurllib.request
ckjar=http.cookiejar.MozillaCookieJar()
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(ckjar))
res=opener.open("http://www.test.com/search.php?ac=account&op=login","username=user&userpwd=pass".encode("utf-8"))
htm=res.read().decode("utf-8")
print(htm)

② 如何用python requests post一段字符串

用requests库发送一次post请求,只要把字符串写在表单里面就可以了。

importrequests
data={key:str}#表单用字典格式,字符串作为value
r=requests.post(url,data=data)

③ python3的requests.get都有哪些请求参数

不用dict,而是用tuple list。实例如下: post_data = [('a', 1), ('a', 2)]requests.post(base_url, data=post_data)

④ 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模块写 下面的post请求

importrequests
url="http://www.xxx.com"
data={"a":"123","b":"456"}
res=requests.post(url,data)
print(res.text)

⑥ 如何创建 python+requests接口自动化测试框架

需要对于读出来的数据进行相应的处理。 当然示例中只是简单列了一下关于POST,GET等二种方式,实际还有很多其它方式,如put,delete等,请求中也还会包括headers,这些都可以自忆添加上去。

⑦ 如何用Python requests 爬取网页所有文字

您可以用requests库的get方法,以请求的网址为参数,获取网页所有html代码,再访问结果是text属性即可。

⑧ 求用python requests库登陆人人的方法示例!

12345678import requestss = requests.session()data = {'email':'用户名','password':'密码'}s.post(',data)r = s.get(")print r.text #打印登陆成功后的首页
编辑器把url格式给改了
再出一个和谐版
把和谐俩字去掉就可以了
123456import requestss = requests.session()data = {'email':'用户名','password':'密码'}s.post('http和谐://www.ren和谐ren.com和谐/PLogin.do',data)r = s.get("和谐http和谐://www.ren和谐ren.com")print r.text

⑨ python requests post 参数重名

不用dict,而是用tuple list。实例如下:

post_data=[('a',1),('a',2)]
requests.post(base_url,data=post_data)

⑩ python http requests 怎么实现模拟登录,提交表单

以下实例是一个完整的代码,实现了从博客获取内容发布至网络,分别实践抓取博客内容、模拟登录、表单提交这几步;
#注意,以下程序是一个完全程序,如果只需要实现模拟登录,提交表单,删除抓取部分即可,相关的代码已经清楚标注,可以根据自己实际情况修改。
代码如下:
# -*- coding: utf-8 -*-
import re
import urllib
import urllib2
import cookielib
#第一步,获取博客标题和正文 ,“IP”可以改为实际地址;
url = "IP"
sock = urllib.urlopen(url)
html = sock.read()
sock.close()
content = re.findall('(?<=blogstory">).*(?=<p class="right artical)', html, re.S)
content = re.findall('<script.*>.*</script>(.*)', content[0], re.S)
title = re.findall('(?<=<title>)(.*)-.* - CSDN.*(?=</title>)', html, re.S)
#根据文章获取内容新建表单值
blog = {'spBlogTitle': title[0].decode('utf-8').encode('gbk'), #文章标题
'spBlogText': content[0].decode('utf-8').encode('gbk'),#文章内容
'ct': "1",
'cm': "1"}
del content
del title

#第二步,模拟登录网络;
cj = cookielib.CookieJar()
#登陆网络的用户名和密码
post_data = urllib.urlencode({'username': '[username]', 'password': '[password]', 'pwd': '1'})
#登录地址路径
path = 'https://passport..com/?login'
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Opera/9.23')]
urllib2.install_opener(opener)
req = urllib2.Request(path, post_data)
conn = urllib2.urlopen(req)

#获取网络登陆认证令牌
bd = urllib2.urlopen(urllib2.Request('http://hi..com/[username]/creat/blog')).read()
bd = re.findall('(?<=bdstoken\" value=\").*(?=ct)', bd, re.S)
blog['bdstoken'] = bd[0][:32]
#设置分类名
blog['spBlogCatName'] = 'php'
#第四步,比较表单,提交表单;req2 = urllib2.Request('http://hi..com/[username]/commit', urllib.urlencode(blog))
#最后,查看表单提交后返回内容,检验;
print urllib2.urlopen(req2).read()
#注意:将[username]/[password]替换为自己真实用户名和密码

阅读全文

与pythonrequests实例相关的资料

热点内容
如何上网上设个人加密账户 浏览:44
linux打开ssh服务 浏览:78
微信位置可以加密吗 浏览:470
算法蛮力法 浏览:438
随机排练命令 浏览:147
python多进程并发 浏览:41
安卓软件安装如何躲避安全检测 浏览:647
奇幻潮翡翠台源码百度云盘 浏览:187
什么软件可以免费pdf转word 浏览:15
php正则表达式大全 浏览:394
androidntp时间 浏览:299
轮机长命令簿英文 浏览:148
oppo铃声设置被加密怎么处理 浏览:548
粤苗app图形验证码怎么填 浏览:899
管家婆架设云服务器 浏览:254
php的登录界面代码 浏览:997
php开发客户端 浏览:998
theisle测试服怎么搜服务器 浏览:447
广播PDF 浏览:218
单片机编程300例汇编百度 浏览:35