import requests
from lxml import etree
import csv
url = 'https://www.58.com/ershoufang/'
headers ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
#爬取页面源码数据
response = requests.get(url=url,headers=headers).text
#数据解析
tree = etree.HTML(response)
#通过页面分析,获取了所有tr标签的对象
tr_list = tree.xpath('//table[@class="tbimg"]/tr')
fp = open('./58.txt','w',encoding='utf-8')
content = []
for tr in tr_list:
#1.房屋基本信息
cont = tr.xpath('./td[2]/a/text()')[0] #之前获取的是单个列表数据,使用[0]表示获取该列表中的第一个字符串
#2.价格
prices = tr.xpath('./td[3]/b/text()')[0]
#3.居室
house = tr.xpath('./td[4]/text()')[0]
#4.大小
size = tr.xpath('./td[5]/b/text()')[0]
content.append([cont,prices,house,size])
#存储CSV
with open('./58.csv','w',encoding='utf-8') as fp:
writer =csv.writer(fp)
writer.writerow(['房屋基本信息','价格','居室','大小'])
for row in content:
writer.writerow(row)
爬取58二手房
最新推荐文章于 2024-11-03 20:27:08 发布