前言
想建一个微信群平时发发淘宝优惠券的商品,当个副业,但是发现经常要自己查看商品,发送链接,实在是太费时间精力(主要就是比较懒),于是萌生了做个脚本,实现自动获取网址商品信息,定时自动发送到群里。
浏览网页信息
首先浏览器打开网页:http://soa.s8bqi.cn/Newcms/html/realtime.html?mi=%E5%B0%8F%E7%BE%8E%E5%A5%BD
按F12查看,因为最终都是在手机端打开的,所以先点击F12出现界面左上角的这个键然后选择network, XHR
运气比较好,直接在图中选中的ssbdlist?id=0找到了当前所有的商品list。
那直接爬取这个网址就可以了,http://mgi.sitezt.cn/api/i/cmsitem/ssbdlist?id=0
验证下是不是包含了商品信息,用浏览器打开试试,如图商品的信息都用json格式很工整的列出来了。
爬取商品list
调用requests得到所有信息,当然我们只需要一部分就够了,
ItemId用来索引商品,ItemTitle商品名称,ItemImage商品图片,
Recommend商品简介
url = 'http://mgi.sitezt.cn/api/i/cmsitem/ssbdlist?id=0'
req=requests.get(url)
data=json.loads(req.content)
先根据关键字简单过滤部分商品,比如水果,然后通过多个商品链接的点击发现,具体的商品详情链接的组合方式,
http://soa.s8bqi.cn/Newcms/html/mobile/goodsDetail.html?pre=realtime&id=(商品ItemId)&mi=%E5%B0%8F%E7%BE%8E%E5%A5%BD
找到商品detail信息
然后发现在这个网页F12–>network–> XHR,还是同样的套路,得到了当前商品的具体信息
http://mgi.sitezt.cn/api/i/cmsitem/getcmsitemdetail?id=613699257749
for goods in data:
if '水果' in goods['ItemTitle']:
source_url = 'http://soa.s8bqi.cn/Newcms/html/mobile/goodsDetail.html?pre=realtime&id='+str(goods['ItemId'])+'&mi=%E5%B0%8F%E7%BE%8E%E5%A5%BD'
goods_info = {}
uu = 'http://mgi.sitezt.cn/api/i/cmsitem/getcmsitemdetail?id=%s'%str(goods['ItemId'])
goods_info['TaoBaoTitle'], goods_info['Recommend'] = get_TaoBaoTitle(uu)
模拟点击得到淘口令
为了在手机微信端打开商品网页的体验更好,所以选择了复制淘口令的方式,但是这就有个问题,在网页你不点击,是无法出现对应的内容的,所以选择selenium + chromedriver 模拟点击事件,代码如下
def get_loulin(url):
mobile_emulation = {"deviceName":"iPhone 6"}
chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument('headless')
chrome_option.add_experimental_option("mobileEmulation", mobile_emulation)
chrome_path = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_option)
driver.get(url)
driver.find_element_by_css_selector('div.koulin-buy').click()
time.sleep(3)
koulin = driver.find_element_by_css_selector('div.m-tkl-c').get_attribute("textContent")
return "復製这条 %s 进入【Tao宝】即可抢购"%koulin
下载商品图片
然后就是下载商品的图片,选择一个本地路径保存图片
def download_pic(img, mkdir_name):
f = open(mkdir_name, 'wb')
r = requests.get(img, timeout=(5))
f.write(r.content)
print(img)
f.close()
到此为止,我们已经得到了需要用于微信发送的所有商品信息,商品图片、商品名称、商品简介、商品淘口令,接下来就是如何通过微信实现自动化定时发送了,将在下一篇展开。
未完待续。。。
代码如下,稍微有点乱,下次再整理下:
import requests
from selenium import webdriver
import io,sys,json
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030')
def get_loulin(url):
mobile_emulation = {"deviceName":"iPhone 6"}
chrome_option = webdriver.ChromeOptions()
chrome_option.add_argument('headless')
chrome_option.add_experimental_option("mobileEmulation", mobile_emulation)
chrome_path = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_option)
driver.get(url)
driver.find_element_by_css_selector('div.koulin-buy').click()
time.sleep(3)
koulin = driver.find_element_by_css_selector('div.m-tkl-c').get_attribute("textContent")
return "復製这条 %s 进入【Tao宝】即可抢购"%koulin
def get_TaoBaoTitle(url):
req=requests.get(url)
data=json.loads(req.content)
return data['TaoBaoTitle'], data['Recommend']
def download_pic(img, mkdir_name):
f = open(mkdir_name, 'wb')
r = requests.get(img, timeout=(5))
f.write(r.content)
print(img)
f.close()
def get_meiguang():
url_data = []
url = 'http://mgi.sitezt.cn/api/i/cmsitem/ssbdlist?id=0'
req=requests.get(url)
data=json.loads(req.content)
index = 0
for goods in data:
if '水果' in goods['ItemTitle']:
source_url = 'http://soa.s8bqi.cn/Newcms/html/mobile/goodsDetail.html?pre=realtime&id='+str(goods['ItemId'])+'&mi=%E5%B0%8F%E7%BE%8E%E5%A5%BD'
goods_info = {}
uu = 'http://mgi.sitezt.cn/api/i/cmsitem/getcmsitemdetail?id=%s'%str(goods['ItemId'])
goods_info['TaoBaoTitle'], goods_info['Recommend'] = get_TaoBaoTitle(uu)
goods_info['koulin'] = get_loulin(source_url)
url_data.append(goods_info)
default_dir = '%s.jpg'%str(index)
index = index + 1
image = goods['ItemImage']
if 'https:' not in goods['ItemImage'] and 'http:' not in goods['ItemImage']:
image = 'https:' + image
download_pic(image, default_dir)
return url_data