Python爬虫,京东自动登录,在线抢购商品

该博客介绍了如何使用Python编写一个京东抢购助手,实现自动登录京东、查询商品信息并进行抢购。通过使用requests和BeautifulSoup4库,该脚本能够模拟用户登录、获取验证码并提交订单。详细代码展示了登录验证、购物车操作和订单详情获取等功能。
摘要由CSDN通过智能技术生成

京东抢购
Python爬虫,自动登录京东网站,查询商品库存,价格,显示购物车详情等。
可以指定抢购商品,自动购买下单,然后手动去京东付款就行。

chang log
2017-03-30 实现二维码扫码登陆

2017-06-27 Golang版JD_AutoBuy

运行环境
Python 2.7

第三方库
Requests: 简单好用,功能强大的Http请求库

beautifulsoup4: HTML文档格式化及便签选择器

环境配置

1 pip install requests
2 pip install beautifulsoup4

项目源码分享

1 # -- coding: utf-8 --
2
3 “”"
4 JD online shopping helper tool
5 -----------------------------------------------------
6 only support to login by QR code,
7 username / password is not working now.
8 “”"
9
10
11 import bs4
12 import requests, requests.utils, pickle
13 import requests.packages.urllib3
14 requests.packages.urllib3.disable_warnings()
15
16 import os
17 import time
18 import json
19 import random
20
21
22 import argparse
23 #from selenium import webdriver
24
25
26 import sys
27 reload(sys)
28 sys.setdefaultencoding(‘utf-8’)
29
30 # get function name
31 FuncName = lambda n=0: sys._getframe(n + 1).f_code.co_name
32
33
34 def tags_val(tag, key=’’, index=0):
35 ‘’’
36 return html tag list attribute @key @index
37 if @key is empty, return tag content
38 ‘’’
39 if len(tag) == 0 or len(tag) <= index:
40 return ‘’
41 elif key:
42 txt = tag[index].get(key)
43 return txt.strip(’ \t\r\n’) if txt else ‘’
44 else:
45 txt = tag[index].text
46 return txt.strip(’ \t\r\n’) if txt else ‘’
47
48
49 def tag_val(tag, key=’’):
50 ‘’’
51 return html tag attribute @key
52 if @key is empty, return tag content
53 ‘’’
54 if tag is None:
55 return ‘’
56 elif key:
57 txt = tag.get(key)
58 return txt.strip(’ \t\r\n’) if txt else ‘’
59 else:
60 txt = tag.text
61 return txt.strip(’ \t\r\n’) if txt else ‘’
62
63
64 class JDWrapper(object):
65 ‘’’
66 This class used to simulate login JD
67 ‘’’
68
69 def init(self, usr_name=None, usr_pwd=None):
70 # cookie info
71 self.trackid = ‘’
72 self.uuid = ‘’
73 self.eid = ‘’
74 self.fp = ‘’
75
76 self.usr_name = usr_name
77 self.usr_pwd = usr_pwd
78
79 self.interval = 0
80
81 # init url related
82 self.home = ‘https://passport.jd.com/new/login.aspx’
83 self.login = ‘https://passport.jd.com/uc/loginService’
84 self.imag = ‘https://authcode.jd.com/verify/image’
85 self.auth = ‘https://passport.jd.com/uc/showAuthCode’
86
87 self.sess = requests.Session()
88
89 self.headers = {
90 ‘User-Agent’ : ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36’,
91 ‘ContentType’: ‘text/html; charset=utf-8’,
92 ‘Accept-Encoding’:‘gzip, deflate, sdch’,
93 ‘Accept-Language’:‘zh-CN,zh;q=0.8’,
94 ‘Connection’ : ‘keep-alive’,
95 }
96
97 self.cookies = {
98
99 }
100
101 ‘’’
102 try:
103 self.browser = webdriver.PhantomJS(‘phantomjs.exe’)
104 except Exception, e:
105 print ‘Phantomjs initialize failed :’, e
106 exit(1)
107 ‘’’
108
109 @staticmethod
110 def print_json(resp_text):
111 ‘’’
112 format the response content
113 ‘’’
114 if resp_text[0] == ‘(’:
115 resp_text = resp_text[1:-1]
116
117 for k,v in json.loads(resp_text).items():
118 print u’%s : %s’ % (k, v)
119
120 @staticmethod
121 def response_status(resp):
122 if resp.status_code != requests.codes.OK:
123 print ‘Status: %u, Url: %s’ % (resp.status_code, resp.url)
124 return False
125 return True
126
127 def _need_auth_code(self, usr_name):
128 # check if need auth code
129 #
130 auth_dat = {
131 ‘loginName’: usr_name,
132 }
133 payload = {
134 ‘r’ : random.random(),
135 ‘version’ : 2015
136 }
137
138 resp = self.sess.post(self.auth, data=auth_dat, params=payload)
139 if self.response_status(resp) :
140 js = json.loads(resp.text[1:-1])
141 return js[‘verifycode’]
142
143 print u’获取是否需要验证码失败’
144 return False
145
146 def get_auth_code(self, uuid):
147 # image save path
148 image_file = os.path.join(os.getcwd(), ‘authcode.jfif’)
149
150 payload = {
151 ‘a’ : 1,
152 ‘acid’ : uuid,
153 ‘uid’ : uuid,
154 ‘yys’ : str(int(time.time() * 1000)),
155 }
156
157 # get auth code
158 r = self.sess.get(self.imag, params=payload)
159 if not self.response_status®:
160 print u’获取验证码失败’
161 return False
162
163 with open (image_file, ‘wb’) as f:
164 for chunk in r.iter_content(chunk_size=1024):
165 f.write(chunk)
166
167 f.close()
168
169 os.system(‘start ’ + image_file)
170 return str(raw_input(‘Auth Code: ‘))
171
172 def login_once(self, login_data):
173 # url parameter
174 payload = {
175 ‘r’: random.random(),
176 ‘uuid’ : login_data[‘uuid’],
177 ‘version’ : 2015,
178 }
179
180 resp = self.sess.post(self.login, data=login_da

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值