爬虫:回龙观租房

#mysqlhelper.py
import pymysql

class MysqlHelper(object):
    def __init__(self):
        self.db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123456', database='py1011', 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 lianjiaxinxi(title) VALUE (%s)', data=('huzeqi hehehe'))
import requests
import re
from lxml import etree
import mysqlhelper

myhelper = mysqlhelper.MysqlHelper()
sql = 'INSERT INTO huilongguan (title, buju, location, status) VALUES (%s, %s, %s, %s)'

for i in range(1,4):
    url = 'https://bj.5i5j.com/zufang/huilongguan/n%s/' %(i)

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36',
        'Cookie': 'PHPSESSID=4qsda5a2lv8k3ls8rc46impdsc; domain=bj; _ga=GA1.2.1446330960.1534510715; _gid=GA1.2.893383312.1534510715; yfx_c_g_u_id_10000001=_ck18081720583916687035717136772; Hm_lvt_94ed3d23572054a86ed341d64b267ec6=1534510728; _Jo0OQK=4786B52FDDFD00A8A3F8FBA45890F5281E90029D464855392F7B77060188839F312A26F03675483B8589CBAB4ED2B2F3F4B96D48A9FE444B19028F1FA8B868E4153C57212F12283777C840763663251ADEB840763663251ADEBB45458312D3818AD2DBEB50F8D291FCBGJ1Z1IQ==; zufang_BROWSES=41291662; yfx_f_l_v_t_10000001=f_t_1534510719651__r_t_1534510719651__v_t_1534512823806__r_c_0; Hm_lpvt_94ed3d23572054a86ed341d64b267ec6=1534513475'
    }

    response = requests.get(url,headers=headers)

    # with open('test.html', 'wb') as f:
    #     f.write(response.content)

    html = response.text
    # print(html)

    html_ele = etree.HTML(response.text)

    li_list = html_ele.xpath('//ul[@class="pList"]/li')
    for li_ele in li_list:
        title = li_ele.xpath('./div[2]/h3/a')[0].text
        print(title)
        buju = li_ele.xpath('./div[2]/div[1]/p[1]/text()')[0]
        print(buju)
        try:
            location = li_ele.xpath('./div[2]/div[1]/p[2]/text()')[0]
        except:
            location = '你SB吗?连地址都不写!?'
        print(location)
        status = li_ele.xpath('./div[2]/div[1]/p[3]/text()')[0]
        print(status)
        print('-'*50)

        data = (title, buju, location, status)
        myhelper.execute_modify_sql(sql, data)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值