python爬取58同城租房信息

python爬取58同城租房信息_第1张图片
代码:

# 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)

python爬取58同城租房信息_第2张图片
python爬取58同城租房信息_第3张图片

你可能感兴趣的:(Python爬虫)