python关于二手房的课程论文_基于python爬取链家二手房信息代码示例
基本環(huán)境配置
python 3.6
pycharm
requests
parsel
time
相關(guān)模塊pip安裝即可
確定目標網(wǎng)頁數(shù)據(jù)
哦豁,這個價格..................看到都覺得腦闊疼
通過開發(fā)者工具,可以直接找到網(wǎng)頁返回的數(shù)據(jù)~
每一個二手房的數(shù)據(jù),都在網(wǎng)頁的 li 標簽里面,咱們可以獲取網(wǎng)頁返回的數(shù)據(jù),然后通過解析,就可以獲取到自己想要的數(shù)據(jù)了~
獲取網(wǎng)頁數(shù)據(jù)
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
解析網(wǎng)頁數(shù)據(jù)
import parsel
selector = parsel.Selector(response.text)
lis = selector.css('.sellListContent li')
dit = {}
for li in lis:
title = li.css('.title a::text').get()
dit['標題'] = title
positionInfo = li.css('.positionInfo a::text').getall()
info = '-'.join(positionInfo)
dit['開發(fā)商'] = info
houseInfo = li.css('.houseInfo::text').get()
dit['房子信息'] = houseInfo
followInfo = li.css('.followInfo::text').get()
dit['發(fā)布周期'] = followInfo
Price = li.css('.totalPrice span::text').get()
dit['售價/萬'] = Price
unitPrice = li.css('.unitPrice span::text').get()
dit['單價'] = unitPrice
csv_writer.writerow(dit)
print(dit)
保存數(shù)據(jù)
import csv
f = open('二手房信息.csv', mode='a', encoding='utf-8-sig', newline='')
csv_writer = csv.DictWriter(f, fieldnames=['標題', '開發(fā)商', '房子信息', '發(fā)布周期', '售價/萬', '單價'])
csv_writer.writeheader()
csv_writer.writerow(dit)
f.close()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持我們。
本文標題: 基于python爬取鏈家二手房信息代碼示例
本文地址: http://www.cppcns.com/jiaoben/python/356366.html
總結(jié)
以上是生活随笔為你收集整理的python关于二手房的课程论文_基于python爬取链家二手房信息代码示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设置夜神模拟器自动连接功能
- 下一篇: Python爬虫——爬取IEEE论文