Crawl Cia Qingcai The Great God's reptile tutorial, and finally save it to MySQL

Source: Internet
Author: User
Tags xpath

#-*-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变量, each time the data is the result of the previous SQL statement, if you want to requery other data, you want to re-write the SQL query statement

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()

Crawl Cia Qingcai The Great God's reptile tutorial, and finally save it to MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.