# -*- coding: utf-8 -*-
#coding:utf8
import requests,time,unittest
from lxml import etree
import pymysql
url =‘http://cuiqingcai.com/1052.html‘
head = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.8"
}
html = requests.get(url)
selector = etree.HTML(str(html.text))
r = selector.xpath("/html/body/section/div[3]/div/article/p/a/text()")
t = selector.xpath("/html/body/section/div[3]/div/article/p/a/@href")
#print(r)
#print(t)
now = str(time.strftime(‘%Y-%m-%d-%H-%M‘,time.localtime(time.time())))
class datas(object):
def Data_storage(self,dt):
for each in dt:
self.uid=",".join(each).split(‘,‘)[0]
self.uname=",".join(each).split(‘,‘)[1]
self.ulink=",".join(each).split(‘,‘)[2]
uid="\‘"+str(self.uid)+"\‘"
uname="\‘"+str(self.uname)+"\‘"
ulink="\‘"+str(self.ulink)+"\‘"
time="\‘"+str(now)+"\‘"
r="\‘"+str(‘ggg‘)+"\‘"
#print(uid,uname,ulink,r,time)
conn=pymysql.connect(host=‘192.168.191.1‘,user=‘root‘,passwd=‘123456789‘,db=‘data‘,port=3306,charset=‘utf8‘)
cur=conn.cursor()#获取一个游标
sql =‘‘‘INSERT INTO xxb(id,name,remark,link,time)VALUES(%s,%s,%s,%s,%s)‘‘‘%(uid,uname,r,ulink,time)
cur.execute(sql)
cur.execute(‘select * from xxb‘)#执行查询sql语句+
data=cur.fetchall()#执行查询后获取的数据赋值给data变量,每次查到的数据是上一个sql语句的结果,如果要重新查询别的数据,则要在上面重新写个sql查询语句
cur.close()#关闭游标
conn.commit()#事务提交
conn.close()#释放数据库资源
def Data_processing(self):
a=[]
for i in range(1,31):
a.append(str(i))
dt = list(zip(a,r,t))
self.Data_storage(dt)
if __name__=="__main__":
gg = datas()
gg.Data_processing()