我爱我家

import requests,time
from lxml import etree
import mysqlhelper

这是我们分页的url, 所以叫base_url 占位符 %s

base_url = ‘https://bj.5i5j.com/zufang/n%s/’
myhelper = mysqlhelper.MysqlHelper()

表名写出 报错 1146 注意!

sql = ‘INSERT INTO 5i5j (title,jiaqian,chuzufangshi,listTag,xiangqing,xiaoqu,guanzhuxinxi) VALUES’ ’ (%s, %s, %s, %s, %s, %s,%s)’

for i in range(1, 3):
url = base_url % i
response = requests.get(url)
# print(response)
html_ele = etree.HTML(response.text)
# print(html_ele)
# print(type(html_ele))
# break
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)

    # 价钱
    jiaqian = li_ele.xpath('./div[2]/div[1]/div[1]/p[1]/strong')[0].text
    print(jiaqian)
    # print(type(jiaqian))

    # 出租方式
    chuzufangshi = li_ele.xpath('./div[2]/div[1]/div[1]/p[2]')[0].text
    print(chuzufangshi)

    # ++++++++listTag1 listTag2 listTag3 listTag4++++++++
    listTag1 = li_ele.xpath('./div[2]/div[2]/span[1]')[0].text
    # print(listTag1)

    listTag2 = li_ele.xpath('./div[2]/div[2]/span[2]')[0].text
    # print(listTag2)


    listTag3 = li_ele.xpath('./div[2]/div[2]/span[1]')[0].text
    # print(listTag3)

    listTag4 = li_ele.xpath('./div[2]/div[2]/span[1]')[0].text
    # print(listTag4)

    # 四句服务  四合一
    listTag = listTag1 + listTag2 + listTag3 + listTag4
    print(listTag)


    #详情
    xiangqing = li_ele.xpath('./div[2]/div[1]/p[1]/text()')
    xiangqing = xiangqing[0]
    print(xiangqing)

    # 小区名字
    xiaoqu = li_ele.xpath('./div[2]/div[1]/p[2]/a[1]')[0].text
    print(xiaoqu)

    # 关注人数
    guanzhuxinxi = li_ele.xpath('./div[2]/div[1]/p[3]/text()')
    guanzhuxinxi = guanzhuxinxi[0]
    print(guanzhuxinxi)

    data = (title,jiaqian,chuzufangshi,listTag,xiangqing,xiaoqu,guanzhuxinxi)
    print(data)
    # print(type(data))
    # break
    myhelper.execute_modify_sql(sql, data)
    time.sleep(3)

你可能感兴趣的:(我爱我家)