我正在嘗試編寫一個腳本,允許我將圖像上傳到BayImg,但我似乎無法讓它正常工作。據我所知,我沒有得到任何結果。我不知道它是否不提交數據或內容,但是當我打印回覆時,我會得到主頁的URL,而不是您上傳圖片時得到的頁面。如果我使用Python 2.x,我會使用Mechanize。但是,它不適用於Py3k,所以我試圖使用urllib。我正在使用Python 3.2.3。這裏是代碼:看不到要獲取POST請求在Python 3中工作
#!/usr/bin/python3
from urllib.parse import urlencode
from urllib.request import Request, urlopen
image = "/test.png"
removal = "remove"
tags = "python script test image"
url = "http://bayimg.com/"
values = {"code" : removal,
"tags" : tags,
"file" : image}
data = urlencode(values).encode("utf-8")
req = Request(url, data)
response = urlopen(req)
the_page = response.read()
任何援助將不勝感激。
按照建議使用請求,效果很好。萬分感謝! – LANshark