我正在执行一个使用Python请求库上传文件的简单任务.我搜索了Stack Overflow,似乎没有人遇到同样的问题,即服务器没有收到该文件:
import requests
url='http://nesssi.cacr.caltech.edu/cgi-bin/getmulticonedb_release2.cgi/post'
files={'files': open('file.txt','rb')}
values={'upload_file' : 'file.txt' , 'DB':'photcat' , 'OUT':'csv' , 'SHORT':'short'}
r=requests.post(url,files=files,data=values)
Run Code Online (Sandbox Code Playgroud)
我正在用我的文件名填充'upload_file'关键字的值,因为如果我把它留空,它会说
Error - You must select a file to upload!
Run Code Online (Sandbox Code Playgroud)
现在我明白了
File file.txt of size bytes is uploaded successfully!
Query service results: There were 0 lines.
Run Code Online (Sandbox Code Playgroud)
仅当文件为空时才会出现.所以我不知道如何成功发送我的文件.我知道该文件有效,因为如果我去这个网站并手动填写表格,它会返回一个很好的匹配对象列表,这就是我所追求的.我非常感谢所有提示.
其他一些线程相关(但没有回答我的问题):
我一直在浏览Python Requests文档,但是我看不到任何我想要实现的功能.
在我的脚本中,我正在设置allow_redirects=True.
我想知道页面是否已被重定向到其他内容,新URL是什么.
例如,如果起始URL是: www.google.com/redirect
最后的URL是 www.google.co.uk/redirected
我如何获得该URL?
之前我使用httplib模块在请求中添加标头.现在我正在尝试与requests模块相同的事情.
这是我正在使用的python请求模块:http: //pypi.python.org/pypi/requests
如何添加标题,request.post并request.get说我必须foobar在标题中的每个请求中添加密钥.
我试图使用Python中的Requests模块发布登录网站的请求,但它并没有真正起作用.我是新手...所以我无法弄清楚我是否应该创建我的用户名和密码cookie或我找到的某种类型的HTTP授权(??).
from pyquery import PyQuery
import requests
url = 'http://www.locationary.com/home/index2.jsp'
Run Code Online (Sandbox Code Playgroud)
所以现在,我想我应该使用"post"和cookies ....
ck = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'}
r = requests.post(url, cookies=ck)
content = r.text
q = PyQuery(content)
title = q("title").text()
print title
Run Code Online (Sandbox Code Playgroud)
我有一种感觉,我做饼干的事情错了......我不知道.
如果它没有正确登录,主页的标题应该出现在"Locationary.com",如果是,它应该是"主页".
如果您可以向我解释有关请求和cookie的一些事情并帮助我解决这个问题,我将非常感激.:d
谢谢.
......它还没有真正起作用.好的......所以这就是主页HTML在您登录之前所说的内容:
</td><td><img src="http://www.locationary.com/img/LocationaryImgs/icons/txt_email.gif"> </td>
<td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName" size="25"></td>
<td><img src="http://www.locationary.com/img/LocationaryImgs/icons/txt_password.gif"> </td>
<td><input class="Data_Entry_Field_Login" type="password" name="inUserPass" id="inUserPass"></td>
Run Code Online (Sandbox Code Playgroud)
所以我认为我做得对,但输出仍然是"Locationary.com"
第二次编辑:
我希望能够保持登录状态很长时间,每当我要求域名下的网页,我希望要显示的内容,就好像我在被记录.
我正在尝试使用请求库发送带有帖子请求的cookie,但我不确定如何根据其文档实际设置cookie.该脚本用于维基百科,需要发送的cookie采用以下形式:
enwiki_session=17ab96bd8ffbe8ca58a78657a918558e; path=/; domain=.wikipedia.com; HttpOnly
Run Code Online (Sandbox Code Playgroud)
但是,requests文档快速入门将此作为唯一示例:
cookies = dict(cookies_are='working')
Run Code Online (Sandbox Code Playgroud)
如何使用此库对上述cookie进行编码?我是否需要使用python的标准cookie库,然后将其与POST请求一起发送?
在我的脚本中,requests.get永远不会返回:
import requests
print ("requesting..")
# This call never returns!
r = requests.get(
"http://www.justdial.com",
proxies = {'http': '222.255.169.74:8080'},
)
print(r.ok)
Run Code Online (Sandbox Code Playgroud)
可能的原因是什么?任何补救措施?get使用的默认超时是多少?
我正在使用python 请求.我需要调试一些OAuth活动,为此我希望它记录所有正在执行的请求.我可以获得这些信息ngrep,但不幸的是,不可能grep https连接(这是必需的OAuth)
如何激活Requests正在访问的所有URL(+参数)的日志记录?
我有一个请求URI和一个令牌.如果我使用:
curl -s "<MY_URI>" -H "Authorization: TOK:<MY_TOKEN>"
Run Code Online (Sandbox Code Playgroud)
等,我得到一个200并查看相应的JSON数据.所以,我安装了请求,当我尝试访问此资源时,我得到403可能是因为我不知道传递该令牌的正确语法.任何人都可以帮我搞清楚吗?这就是我所拥有的:
import sys,socket
import requests
r = requests.get('<MY_URI>','<MY_TOKEN>')
r. status_code
Run Code Online (Sandbox Code Playgroud)
我已经尝试过:
r = requests.get('<MY_URI>',auth=('<MY_TOKEN>'))
r = requests.get('<MY_URI>',auth=('TOK','<MY_TOKEN>'))
r = requests.get('<MY_URI>',headers=('Authorization: TOK:<MY_TOKEN>'))
Run Code Online (Sandbox Code Playgroud)
但这些都不起作用.
我在我的Python脚本中使用了很棒的Requests库:
import requests
r = requests.get("some-site.com")
print r.text
Run Code Online (Sandbox Code Playgroud)
我想用socks代理.但是Requests现在只支持HTTP代理.
我怎样才能做到这一点?
python ×10
python-requests ×10
get ×2
cookies ×1
file ×1
file-upload ×1
http ×1
http-headers ×1
http-request ×1
logging ×1
proxy ×1
pyquery ×1
redirect ×1
soap ×1
socks ×1
token ×1