Skip to content

Commit

Permalink
fix open QR image by OS, fix buy count not work for third party vender
Browse files Browse the repository at this point in the history
  • Loading branch information
adyzng committed Nov 23, 2017
1 parent 389d769 commit 31b71e6
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions scraper-jd.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,18 @@ def login_by_QR(self):
f.write(chunk)

## scan QR code with phone
os.system('start ' + image_file)
if os.name == "nt":
# for windows
os.system('start ' + image_file)
else:
if os.uname()[0] == "Linux":
# for linux platform
os.system("xdg-open " + image_file)
else:
# for Mac platform
os.system("open " + image_file)

# step 3 check scan result
# step 3: check scan result
## mush have
self.headers['Host'] = 'qr.m.jd.com'
self.headers['Referer'] = 'https://passport.jd.com/new/login.aspx'
Expand Down Expand Up @@ -408,7 +417,7 @@ def good_stock(self, stock_id, good_count=1, area_id=None):
# http://c0.3.cn/stocks?callback=jQuery2289454&type=getstocks&skuIds=3133811&area=1_72_2799_0&_=1490694504044
# jQuery2289454({"3133811":{"StockState":33,"freshEdi":null,"skuState":1,"PopType":0,"sidDely":"40","channel":1,"StockStateName":"现货","rid":null,"rfg":0,"ArrivalDate":"","IsPurchase":true,"rn":-1}})
# jsonp or json both work
stock_url = 'http://c0.3.cn/stocks'
stock_url = 'https://c0.3.cn/stocks'

payload = {
'type' : 'getstocks',
Expand All @@ -433,7 +442,7 @@ def good_stock(self, stock_id, good_count=1, area_id=None):
return stock_stat, stock_stat_name

except Exception as e:
print 'Exception:', e
print 'Stocks Exception:', e
time.sleep(5)

return (0, '')
Expand Down Expand Up @@ -487,7 +496,7 @@ def good_detail(self, stock_id, area_id=None):
print u'库存:{0}'.format(good_data['stockName'])
print u'价格:{0}'.format(good_data['price'])
print u'名称:{0}'.format(good_data['name'])
print u'链接:{0}'.format(good_data['link'])
#print u'链接:{0}'.format(good_data['link'])

return good_data

Expand Down Expand Up @@ -540,6 +549,10 @@ def buy(self, options):
return False

try:
# change buy count
if options.count != 1:
link = link.replace('pcount=1', 'pcount={0}'.format(options.count))

# add to cart
resp = self.sess.get(link, cookies = self.cookies)
soup = bs4.BeautifulSoup(resp.text, "html.parser")
Expand All @@ -555,10 +568,11 @@ def buy(self, options):

print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++'
print u'{0} > 购买详情'.format(time.ctime())
print u'链接:{0}'.format(link)
print u'结果:{0}'.format(tags_val(tag))

# change count
self.buy_good_count(options.good, options.count)
# change count after add to shopping cart
#self.buy_good_count(options.good, options.count)

except Exception, e:
print 'Exp {0} : {1}'.format(FuncName(), e)
Expand All @@ -572,7 +586,7 @@ def buy_good_count(self, good_id, count):
url = 'http://cart.jd.com/changeNum.action'

payload = {
'venderId': '8888',
#'venderId': '8888',
'pid': good_id,
'pcount': count,
'ptype': '1',
Expand Down

0 comments on commit 31b71e6

Please sign in to comment.