from selenium import webdriver from scrapy.selector import Selector import time import random import pymysql from urllib import parse import re import os # title='30天内已售出234件,其中交易成功64件' # a=re.findall('d{1,}',title) # print(a[1]) conection = pymysql.connect(host='localhost',user='root',password='123',db='7.27',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor) sql='''SELECT `商品id`.id, `上架时间`,'1天销量' as 日期 FROM `商品id` WHERE TIMESTAMPDIFF( DAY, `上架时间`, CURDATE() ) = 1 AND `品牌` = '安妮森林' UNION SELECT `商品id`.id, `上架时间`,'7天销量' as 日期 FROM `商品id` WHERE TIMESTAMPDIFF(DAY,`上架时间`,CURDATE()) =7 AND `品牌`= '安妮森林' ''' with conection.cursor() as cursor: cursor.execute(sql) shop_id = cursor.fetchall() shop_olxx = [i for i in shop_id] print(shop_olxx) conection.commit() webdriver = webdriver.Ie() for i in shop_olxx: webdriver.implicitly_wait(200) webdriver.get('http://item.taobao.com/item.htm?id=' + i['id']) myDynamicElement = webdriver.find_element_by_class_name('tb-price-spec') time.sleep(random.randrange(2, 6)) date = webdriver.page_source select_xixi = Selector(text=date) pice=select_xixi.xpath('//*[@id="J_Counter"]/div/div[2]/a/@title').re('d{1,}')[1] cursor = conection.cursor() sql = "UPDATE 商品id set " + i['日期'] + " = (%s) where id = %s" cursor.execute(sql, (pice, i['id'])) conection.commit()