京东抢购
Python爬虫,自动登录京东网站,查询商品库存,价格,显示购物车详情等。
可以指定抢购商品,自动购买下单,然后手动去京东付款就行。
chang log
-
2017-03-30 实现二维码扫码登陆
-
2017-06-27 Golang版JD_AutoBuy
运行环境
Python 2.7
第三方库
-
Requests: 简单好用,功能强大的Http请求库
-
beautifulsoup4: HTML文档格式化及便签选择器
环境配置
pip install requests
pip install beautifulsoup4
ps:推荐一下我建的python零基础系统学习交流扣扣qun:前面是934,中间109,后面是170,群里有免费的视频教程,开发工具、电子书籍分享。专业的老师答疑!学习python web、python爬虫、数据分析、大数据,人工智能等技术有不懂的可以加入一起交流学习,一起进步!
项目源码分享
# -*- coding: utf-8 -*-
"""
JD online shopping helper tool
-----------------------------------------------------
only support to login by QR code,
username / password is not working now.
"""
import bs4
import requests, requests.utils, pickle
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
import os
import time
import json
import random
import argparse
#from selenium import webdriver
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# get function name
FuncName = lambda n=0: sys._getframe(n + 1).f_code.co_name
def tags_val(tag, key='', index=0):
'''
return html tag list attribute @key @index
if @key is empty, return tag content
'''
if len(tag) == 0 or len(tag) <= index:
return ''
elif key:
txt = tag[index].get(key)
return txt.strip(' \t\r\n') if txt else ''
else:
txt = tag[index].text
return txt.strip(' \t\r\n') if txt else ''
def tag_val(tag, key=''):
'''
return html tag attribute @key
if @key is empty, return tag content
'''
if tag is None:
return ''
elif key:
txt = tag.get(key)
return txt.strip(' \t\r\n') if txt else ''
else:
txt = tag.text
return txt.strip(' \t\r\n') if txt else ''
class JDWrapper(object):
'''
This class used to simulate login JD
'''
def __init__(self, usr_name=None, usr_pwd=None):
# cookie info
self.trackid = ''
self.uuid = ''
self.eid = ''
self.fp = ''
self.usr_name = usr_name
self.usr_pwd = usr_pwd
self.interval = 0
# init url related
self.home = 'https://passport.jd.com/new/login.aspx'
self.login = 'https://passport.jd.com/uc/loginService'
self.imag = 'https://authcode.jd.com/verify/image'
self.auth = 'https://passport.jd.com/uc/showAuthCode'
self.sess = requests.Session()
self.headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
'ContentType': 'text/html; charset=utf-8',
'Accept-Encoding':'gzip, deflate, sdch',
'Accept-Language':'zh-CN,zh;q=0.8',
'Connection' : 'keep-alive',
}
self.cookies = {
}
'''
try:
self.browser = webdriver.PhantomJS('phantomjs.exe')
except Exception, e:
print 'Phantomjs initialize failed :', e
exit(1)
'''
@staticmethod
def print_json(resp_text):
'''
format the response content
'''
if resp_text[0] == '(':
resp_text = resp_text[1:-1]
for k,v in json.loads(resp_text).items():
print u'%s : %s' % (k, v)
@staticmethod
def response_status(resp):
if resp.status_code != requests.codes.OK:
print 'Status: %u, Url: %s' % (resp.status_code, resp.url)
return False
return True
def _need_auth_code(self, usr_name):
# check if need auth code
#
auth_dat = {
'loginName': usr_name,
}
payload = {
'r' : random.random(),
'version' : 2015
}
resp = self.sess.post(self.auth, data=auth_dat, params=payload)
if self.response_status(resp) :
js = json.loads(resp.text[1:-1])
return js['verifycode']
print u'获取是否需要验证码失败'
return False
def _get_auth_code(self, uuid):
# image save path
image_file = os.path.join(os.getcwd(), 'authcode.jfif')
payload = {
'a' : 1,
'acid' : uuid,
'uid' : uuid,
'yys' : str(int(time.time() * 1000)),
}
# get auth code
r = self.sess.get(self.imag, params=payload)
if not self.response_status(r):
print u'获取验证码失败'
return False
with open (image_file, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
f.write(chunk)
f.close()
os.system('start ' + image_file)
return str(raw_input('Auth Code: '))
def _login_once(self, login_data):
# url parameter
payload = {
'r': random.random(),
'uuid' : login_data['uuid'],
'version' : 2015,
}
resp = self.sess.post(self.login, data=login_data, params=payload)
if self.response_status(resp):
js = json.loads(resp.text[1:-1])
#self.print_json(resp.text)