日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python爬取58同城租房信息

系統 1982 0

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張圖片


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 东丽区| 濮阳县| 迁安市| 蒙山县| 顺平县| 河津市| 佛学| 额敏县| 达拉特旗| 内丘县| 余庆县| 三门县| 沽源县| 镇赉县| 蓬溪县| 阳江市| 新干县| 贞丰县| 即墨市| 泸水县| 云和县| 临泉县| 余庆县| 昆明市| 兴城市| 松阳县| 大宁县| 赣州市| 金华市| 南靖县| 南川市| 象州县| 波密县| 绵阳市| 苗栗县| 聂荣县| 邳州市| 澳门| 嘉善县| 泉州市| 大洼县|