python爬取58同城租房信息

不羁的心 提交于 2019-12-08 21:59:25

在这里插入图片描述
代码:

# coding=utf-8
import sys
import csv
import requests
from bs4 import BeautifulSoup
'''
遇到不懂的问题?Python学习交流群:821460695满足你的需求,资料都已经上传群文件,可以自行下载!
'''
reload(sys)
sys.setdefaultencoding('utf-8')
# 请求头设置

def download(url):
    db_data = requests.get(url)
    soup = BeautifulSoup(db_data.text, 'lxml')
    titles = soup.select(
        'body > div.mainbox > div.main > div.content > div.listBox > ul > li > div.des > h2 > a:nth-of-type(1)')
    houses = soup.select('body > div.mainbox > div.main > div.content > div.listBox > ul > li > div.des > p.room')
    oneaddresss = soup.select(
        'body > div.mainbox > div.main > div.content > div.listBox > ul > li > div.des > p.add > a:nth-of-type(1)')
    twoaddresss = soup.select(
        'body > div.mainbox > div.main > div.content > div.listBox > ul > li > div.des > p.add > a:nth-of-type(2)')
    prices = soup.select(
        'body > div.mainbox > div.main > div.content > div.listBox > ul > li > div.listliright > div.money > b')
    for title, house, oneaddress, twoaddress, price in zip(titles, houses, oneaddresss, twoaddresss, prices):
        data = [
            (
                str(title.string).replace(' ', '').replace('\n', ''),
                house.get_text().split(' ')[0].replace(' ', '').replace("\n", ""),
                house.get_text().split(' ')[-1].replace(' ', '').replace("\n", ""),
                oneaddress.get_text().replace(' ', '').replace("\n", ""),
                twoaddress.get_text().replace(' ', '').replace("\n", ""),
                price.get_text().replace(' ', '').replace("\n", "")
            )
        ]

        csvfile = open('kf.csv', 'ab')
        writer = csv.writer(csvfile)
        print('write one house')
        writer.writerows(data)
        csvfile.close()


# 初始化csv文件
def info():
    csvinfo = open('kf.csv', 'ab')
    begcsv = csv.writer(csvinfo)
    begcsv.writerow(['title', 'house', 'area', 'address1', 'address2', 'price'])
    csvinfo.close()


if __name__ == '__main__':
    info()
    download(url)

在这里插入图片描述
在这里插入图片描述

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!