妹子图

import requests
from lxml import etree
import mysql_Helper
myhelper = mysql_Helper.MysqlHelper()
sql = 'INSERT INTO mzitu (li_bbb) VALUES' \
      ' (%s)'

# url = 'http://www.mzitu.com/page/%s/'
# response = requests.get(url)

# with open('mzitu.com','wb') as f:
#     f.write(response.content)

headers = {
    "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.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Cookie": "Hm_lvt_dbc355aef238b6c32b43eacbbf161c3c=1534477817,1534554821,1534677508; Hm_lpvt_dbc355aef238b6c32b43eacbbf161c3c=1534678068",
    "Host": "www.mzitu.com",
    "Pragma": "no-cache",
    "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
}

base_url = 'http://www.mzitu.com/page/%s/'
for i in range(1,4):
    url = base_url % i
    response = requests.get(url,headers=headers)
    html_ele = response.text
    # print(html_str)
    ver = etree.HTML(html_ele)
    li_list = ver.xpath('//div[@class="postlist"]/ul/li/a/@href')
    # print(li_list)
    for li in li_list:
        # print(li)
        url_tu = requests.get(li)
        # print(url_tu)
        www= url_tu.text
        aaa = etree.HTML(www)
        # print(aaa)
        li_aaa = aaa.xpath('//div[@class="pagenavi"]/a[5]/span')[0].text
        # print(li_aaa)
        # print(li_bbb)
        for ccc in range(1,int(li_aaa)+1):
            sss =  str(li) + '/'+str(ccc)
            # print(sss)
            bbss = requests.get(sss)
            uuu = bbss.text
            lll = etree.HTML(uuu)
            li_bbb = lll.xpath('//div[@class="main-image"]/p/a/img/@src')[0]
            print(li_bbb)
            # break
            data = (li_bbb)
            myhelper.execute_modify_sql(sql, data)

# /html/body/div[2]/div[1]/div[4]/a[6]/span
import pymysql

class MysqlHelper(object):
    def __init__(self):
        self.db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='lxh1122', database='py11', charset='utf8')

        # self.db = pymysql.connect(host='127.0.0.1',port='3306',user='root',password='lxh1122',database='py11',charset='utf8')
        self.cursor = self.db.cursor()
    def execute_modify_sql(self,sql,data):
        self.cursor.execute(sql,data)
        self.db.commit()
    def __del__(self):
        self.cursor.close()
        self.db.close()
if __name__=='__main__':
    conn = MysqlHelper()
    conn.execute_modify_sql('insert into wawj(title) VALUE (%s)', data=('aabbccdd'))

猜你喜欢

转载自www.cnblogs.com/lxh777/p/9503227.html
今日推荐