python爬取安居客保存mysql

import requests
from lxml import etree
import pymysql

url = 'https://bj.zu.anjuke.com/fangyuan/huilongguan/p{}/'

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}


# 连接数据库
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123456', database='python', charset='utf8')
# 创建游标
cursor = db.cursor()
# 发送请求


for p in range(3):
    p+=1
    url=url.format(p)
    print(p)
    response = requests.get(url, headers=headers)
    # 网页保存到本地 测试用
    # with open('anjuke.html', 'wb' ) as f:
    #     f.write(response.content)
    html_text = response.text
    # print(html_text)
    # 获取网页的obj
    html_obj = etree.HTML(html_text)
    # print(html_obj)
    html_list = html_obj.xpath('//div[@class="list-content"]/div')
    for i in html_list:
        if i.xpath('./div[1]/h3/a'):
            # 标题
            title = i.xpath('./div[1]/h3/a')[0].text
            print(title)
            # 庭室
            ts = i.xpath('./div[1]/p[1]/text()[1]')[0]
            room = ''.join(ts.split())
            print(room)
            # 大小 平米
            size = i.xpath('./div[1]/p[1]/text()[2]')[0]
            print(size)
            # 楼层
            floor = i.xpath('./div[1]/p[1]/text()[3]')[0]
            print(floor)

            # 地址 + 小区
            addres = i.xpath('./div[1]/address/a')[0].text
            addres1 = i.xpath('./div[1]/address/text()')[1]
            addres2 = addres1+'/'+addres
            # 有空格 进行切割 然后在拼接
            #  函数split() 不传参就以空格分裂为list  ['昌平-回龙观', '回龙观西大街', '/龙华园(西区)']
            # print(addres2.split())
            # 函数join() 前面是以什么连接为str   昌平-回龙观 回龙观西大街 /龙华园(西区)
            # print(' '.join(addres2.split()))
            address = ' '.join(addres2.split())
            # print(addres3)

            # 房租情况
            info = i.xpath('./div[1]/p[2]/span/text()')
            info = '/'.join(info)
            print(info)
            # 房租价格
            price = i.xpath('./div[2]/p[1]/strong/text()')[0]
            print(price)
            sql = '''insert into anjuke VALUES (null,'{}','{}','{}','{}','{}','{}','{}')'''.format(title,price,room,size,floor,address,info)
            cursor.execute(sql)
            db.commit()


cursor.close()
db.close()




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值