Python爬取58同城二手房資訊的標題名稱

2021-10-24 08:55:15 字數 936 閱讀 2226

今天,我們用python來爬取58同城頁面二手房資訊的資料。

首先開啟**:

# 爬取頁面原始碼資料

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

.text

# 資料解析

tree = etree.html(page_text)

# 儲存li標籤物件

li_list = tree.xpath(

'//ul[@class="house-list-wrap"]/li'

) fp =

open

('data/58同城二手房.txt'

,'w'

,encoding=

'utf-8'

)for li in li_list:

# 以li標籤為跟標籤定位

title = li.xpath(

'./div[2]/h2/a/text()')[

0]print

(title)

fp.write(title+

'\n'

)執行結果如下:

用xpath爬取58同城二手房並存入csv檔案中

import requests from lxml import etree import csv 爬取58同城二手房 if name main headers fp open d python 58.csv w encoding utf8 newline csv writer csv.writer...

爬取二手房資訊

開源到github了 專案位址 基於springboot,idea 匯入依賴 org.jsoupgroupid jsoupartifactid 1.10.2version dependency 資料放入redis中,引人redis org.springframework.bootgroupid sp...

爬取58二手房的放原標題

import requests from bs4 import beautifulsoup import re from lxml import etree import time 需求 爬取58二手房的 資訊 if name main headers 爬取到頁面原始碼資料 url page tex...