我正在制作一个与Steam市场(http://steamcommunity.com/market)互动的Python机器人。一切进展顺利,但我仍然坚持创建买单。我将Python(3)代码基于以下javascript:
import smtplib
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login('surapon','222222')
mail.sendmail('surapon@gmail.com','youremail@gmail.com',content)
mail.quit
其中currency / appid / price_total / quantity是整数,sessionid和market_hash_name是字符串。我已将此代码重构为Python 3:
$J.ajax( {
url: 'https://steamcommunity.com/market/createbuyorder/',
type: 'POST',
data: {
sessionid: g_sessionID,
currency: g_rgWalletInfo['wallet_currency'],
appid: this.m_unAppId, // ITEM?
market_hash_name: this.m_strMarketHashName,
price_total: price_total,
quantity: quantity
},
crossDomain: true,
xhrFields: { withCredentials: true }
} ).done( function ( data ) {
CreateBuyOrderDialog.OnCreateBuyOrderComplete( { responseJSON: data } );
} ).fail( function( jqxhr ) {
// jquery doesn't parse json on fail
var data = $J.parseJSON( jqxhr.responseText );
CreateBuyOrderDialog.OnCreateBuyOrderComplete( { responseJSON: data } );
} );
但是,在调用cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
#before the request is made, cookies are 'created' by doing other requests on the same website
def placeOrder():
url = 'http://steamcommunity.com/market/createbuyorder'
values = {'sessionid' : self.sessionid,
'currency' : '3',
'appid' : '730',
'market_hash_name' : 'Chroma 2 Case',
'price_total' : '4',
'quantity' : '1'}
headers = {'Accept' : '*/*',
'Content-type' : 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer' : 'http://steamcommunity.com/market/listings/730/Chroma%202%20Case',
'Accept-Language' : 'nl-NL',
'Origin' : 'http://steamcommunity.com',
'Accept-Encoding' : 'gzip, deflate',
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
'Host' : 'steamcommunity.com',
'Connection' : 'Keep-Alive',
'Cache-Control' : 'no-cache'}
post = urllib.parse.urlencode(values)
binary_data = post.encode('utf-8')
request = urllib.request.Request(url, binary_data, headers)
response = urllib.request.urlopen(request)
data = json.loads(response.decode('utf-8'))
函数时,会返回此错误:placeOrder()
。
为什么这会产生错误的请求?因为我通过创建一个真实的'来完全复制标题和cookie。通过购买商品请求此网址http://steamcommunity.com/market/listings/730/Chroma%202%20Case#。
为什么网站会返回错误的请求?什么是最有可能的? (例如,丢失cookie,丢失标题,错过步骤等)。
非常感谢帮助!
答案 0 :(得分:0)
url
,http://steamcommunity.com/market/createbuyorder
在Python代码中缺少尾随/
,这是一个区别。
难以调试,发送的数据是否相同?
答案 1 :(得分:0)
也许它迟到但我得到了一些建议。
Ajax请求中的URL是" https"不是" http"在这里可能很重要,错误请求的另一个可能原因可能是凭据,尝试使用标头:
"Access-Control-Allow-Origin": "http://steamcommunity.com"
和"Access-Control-Allow-Credentials": "true"
。请告诉我你是否解决了问题以及如何解决问题。