Python爬取售房信息并保存至CSV文件

Python爬取售房信息并保存至CSV文件


在上一篇文章: Python爬取租房信息并保存至Excel文件,介绍了如何使用Python爬取租房信息并保存至Excel文件,在本案例中则是使用Python爬取售房信息并保存至CSV文件。与之前相比,数据的提取方式有所不同,这里用到了Selector选择器,而数据保存的目标文件则是CSV文件。

  • 相关代码如下:
import requests
import parsel
import csv
import time

f = open('静安区售房信息.csv', mode='a', encoding='utf_8_sig', newline='')
csv_write = csv.DictWriter(f, fieldnames=['标题', '地址', '户型', '面积', '朝向', '装修', '楼层', '年代', '关注及发布', '其它', '总价', '单价', '详情'])
csv_write.writeheader()

for page in range(1, 29):
    time.sleep(3)
    print(f'======================正在爬取第{page}页数据内容======================')
    url = f'https://sh.lianjia.com/ershoufang/jingan/pg{page}/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36'}
    response = requests.get(url=url, headers=headers)
    # print(response.text)
    selector = parsel.Selector(response.text)
    divs = selector.css('div.info.clear')
    # print(divs)
    for div in divs:
        title = div.css('.title a::text').get()
        area_list = div.css('.positionInfo a::text').getall()
        area = '-'.join(area_list)
        house_info = div.css('.houseInfo::text').get().split('|')
        house_type = house_info[0]
        house_area = house_info[1]
        house_face = house_info[2]
        decoration = house_info[3]
        floor = house_info[4]
        years = house_info[5]
        follow_info = div.css('.followInfo::text').get().replace(' / ', ',')
        tag_list = div.css('.tag span::text').getall()
        tag = '|'.join(tag_list)
        totalprice = div.css('.totalPrice span::text').get() + '万'
        unitprice = div.css('.unitPrice span::text').get().replace('单价', '')
        href = div.css('.title a::attr(href)').get()
        dit = {
            '标题': title,
            '地址': area,
            '户型': house_type,
            '面积': house_area,
            '朝向': house_face,
            '装修': decoration,
            '楼层': floor,
            '年代': years,
            '关注及发布': follow_info,
            '其它': tag,
            '总价': totalprice,
            '单价': unitprice,
            '详情': href,
        }
        csv_write.writerow(dit)
        print(title, area, house_type, house_area, house_face, decoration, floor, years, follow_info, tag, totalprice,
              unitprice, href, sep='|')
print("爬取完毕!")


  • 17
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将爬取的网页内容保存CSV文件中,可以使用Python中的csv模块。具体步骤如下: 1. 导入相关模块: ```python import requests import csv from bs4 import BeautifulSoup ``` 2. 发送HTTP请求,获取网页内容: ```python url = 'http://www.example.com' response = requests.get(url) html_content = response.content ``` 3. 解析HTML内容,获取需要的信息: ```python soup = BeautifulSoup(html_content, 'html.parser') title = soup.title.string text = soup.get_text() ``` 4. 将获取的信息写入CSV文件: ```python with open('example.csv', 'w', encoding='utf-8', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Title', 'Text']) writer.writerow([title, text]) ``` 注意事项: - `csv.writer()`中`newline=''`参数的作用是防止写入CSV文件时出现空行; - CSV文件的编码一般为`utf-8`,中文字符需要特别注意编码问题。 完整代码示例: ```python import requests import csv from bs4 import BeautifulSoup url = 'http://www.example.com' response = requests.get(url) html_content = response.content soup = BeautifulSoup(html_content, 'html.parser') title = soup.title.string text = soup.get_text() with open('example.csv', 'w', encoding='utf-8', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Title', 'Text']) writer.writerow([title, text]) ``` 执行完毕后,当前目录下会生成一个名为`example.csv`的文件文件内容为爬取的网页标题和文本。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值