2016-02-28 56 views
-1

我试图让我的形象在线托管和我使用的python如何发送从蟒蛇的请求后响应,图像

import requests 
url = 'http://imgup.net/' 
data = {'image[image][]':'http://www.webhost-resources.com/wp-content/uploads/2015/01/dedicated-hosting-server.jpg'} 
r = requests.post(url, files=data) 

我不能够从获得托管的图片的反应网址响应 。 请帮忙!

+0

你得到了什么错误? – Arman

+0

r.text给我同样的html页面,它不包含来自服务器端的post响应! –

+0

如果你尝试donwload一个图像使用[beautifulSoup](http://stackoverflow.com/questions/18497840/beautifulsoup-how-to-open-images-and-download-them) – Arman

回答

1
  1. requests.postfiles参数需要:的'name': file-like-objects(或{'name': ('filename', fileobj)}),用于多编码上传

字典。

  • 有更多的数据,你需要的不仅仅是要发送的文件,最重要的是“真实性令牌”。如果您查看页面的源代码,它会向您显示所有其他参数,如<input type="hidden">标签。

  • 上传网址是http://imgup.net/upload,您可以从action属性<form>中看到。


  • 所以,你需要做的是:

    1. 下载你要上传的图片(我把它叫做dhs.jpg)。

    2. 做主页的GET请求,提取authenticity_token

    3. 一旦你的,与files=data=发送请求:

    url = "http://imgup.net/upload" 
    data = {'utf8': '&#x2713;', 'authenticity_token': '<put your scraped token here>', '_method': 'put'} 
    f = open("dhs.jpg", "rb") # open in binary mode 
    files = {'image[image][]': f} 
    r = requests.post(url, files=files, data=data) 
    f.close() 
    print(r.json()["image_link"] 
    

    最后一点:虽然我找不到反对这种任何规则行为在他们的T & C中,真实性令牌的存在使得imgup似乎并不真的希望你自动执行此操作

    +0

    如果我的问题很清楚,你能否解决这个问题? –

    +0

    为什么这个答案downvoted? – themantalope

    +1

    我认为没有真实性标记。请纠正我,如果它是非法的,因为代码没有任何身份验证工作谢谢兄弟! –