我正在用 Python 编写一个 IRC 机器人,以便我和我的朋友从 Steam 市场购买物品。它主要是为了好玩和提高我的编程技能。我已经能够获得一件商品的最低价格,但我在发布购买请求时遇到了问题。我发出了会话 id 和 steamLogin cookie,url 可以是http://steamcommunity.com/market/listings/730/Winter%20Offensive%20Weapon%20Case
. 这是蒸汽部分的代码:
import mechanize, re, datetime, urllib
from cookielib import Cookie
from cookielib import LWPCookieJar
url = 'http://steamcommunity.com/market/listings/730/blahblah'
sessionid = ''
steamLogin = ''
maximum = int(raw_input('How much are you willing to pay? '))
def makeCookie(name, value):
return Cookie(
version=0,
name=name,
value=value,
port=None,
port_specified=False,
domain="steamcommunity.com",
domain_specified=True,
domain_initial_dot=False,
path="/",
path_specified=True,
secure=False,
expires=None,
discard=False,
comment=None,
comment_url=None,
rest=None
)
br = mechanize.Browser()
cj = LWPCookieJar()
br.set_cookiejar(cj)
cj.set_cookie(makeCookie('sessionid', sessionid))
cj.set_cookie(makeCookie('steamLogin', steamLogin))
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7')]
id = ''
response = br.open(url)
elements = response.read().splitlines()
line = (elements[elements.index('\tvar g_rgCurrency = [];') + 1])[23:]
match = re.search(r'("listingid":")(\d+)(.+?)(converted_price":)(\d+)(,"converted_fee":)(\d+)', line)
if not match: continue
if match.group(2) == id: continue
id = match.group(2)
subtotal = int(match.group(5))
fee = int(match.group(7))
total = subtotal + fee
print "Total at", str(datetime.datetime.now()) + ' for listing id (' + id + '):', '${:,.2f}'.format(float(total)/100)
if total <= maximum:
data = urllib.urlencode({'sessionid': sessionid,
'currency': '1',
'subtotal': subtotal,
'fee': fee,
'total': total})
print data
try:
post = br.open('https://steamcommunity.com/market/buylisting/' + id, data)
except Exception as e:
print e