from pyquery import PyQuery as pq
import re
import pymysql

def huoqushuju():
    for num in range(1,10):
        #请求信息
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
        list_url = "https://url...."+ str(num) + "....."
        doc = pq(url=list_url,encoding='utf-8',headers=headers)
        items = doc('.list-ershou')
        lis = items.find('li').items()
        list=[]
        for i in lis:
            xiaoqu = i('.xicon-xq').siblings().text()
            weizhi = i('.xicon-addr').siblings().text()
            xiangqing = i('.list-houstTab').text()
            guige = re.findall('\s\d室\s\d厅\s\d卫', xiangqing)
            if not guige:
                guige = 'null'
            louceng = re.findall('第\d+层\s共\d+层', xiangqing)
            if not louceng:
                louceng = 'null'
            fangling = re.findall('房龄\s\d+年', xiangqing)
            if not fangling:
                fangling = 'null'
            zongjia = i('.price1').text()
            zongjia = re.findall('\d+', zongjia)
            zhongjie = i('.tab').eq(1).text()
            try:
                zhongjie = re.split(':',zhongjie)[1]
            except:
                zhongjie ='null'
            jiage = i('.price2').text()
            jiage = re.findall('\d+',jiage)
            # print(xiaoqu, zongjia, jiage, weizhi, louceng, guige, fangling, zhongjie)

            db = pymysql.connect(host='localhost',user='root',password='123123',port=3306, db='test')
            cursor = db.cursor()
            sql = 'insert into ershoufang2 (xiaoqu,zongjia,jiage,weizhi,louceng,guige,fangling,zhongjie) values(%s,%s,%s,%s,%s,%s,%s,%s)'
            # cursor.execute(sql, (xiaoqu, zongjia, jiage, weizhi, louceng, guige, fangling, zhongjie))
            try:
                cursor.execute(sql, (xiaoqu,zongjia,jiage,weizhi,louceng,guige,fangling,zhongjie))
                db.commit()
                print('插入成功!')
            except:
                print('插入失败!!!!!!!!!!!!!!!!')
                db.close()

if __name__ == '__main__':
    huoqushuju()

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

open