爬取安居客上住房信息的简单爬虫 ,并存储为表格文件

编程入门 行业动态 更新时间:2024-10-23 03:21:35

爬取安居客上住房信息的简单<a href=https://www.elefans.com/category/jswz/34/1770264.html style=爬虫 ,并存储为表格文件"/>

爬取安居客上住房信息的简单爬虫 ,并存储为表格文件

代码如下,有注释进行介绍:

#本爬虫仅用于学习,纯属爱好,虽然本爬虫很简单,但还是请大家不要滥用
#python3, Firefox浏览器import requests
from bs4 import BeautifulSoup
import time
import csv# 定制请求头,请求头在浏览器中查看,具体方法见附录一
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0',
}# 将要访问的网址
link = '/'
# 访问该网站
r = requests.get(link, headers=headers, timeout=100)# 使用BeautifulSoup提取html中的内容
# BeautifulSoup 中文官方文档:.zh/#id37
soup = BeautifulSoup(r.text, 'lxml')
house_list = soup.find_all('li', class_="list-item")# 将爬取的内容写入 test.csv中,编码格式为 'UTF-8'
with open('test.csv', 'a+', encoding='UTF-8', newline='') as csvfile:w = csv.writer(csvfile)for house in house_list:temp = []name = house.find('div', class_="house-title").a.text.strip()price = house.find('span', class_='price-det').text.strip()price_area = house.find('span', class_='unit-price').text.strip()no_room = house.find('div', class_='details-item').span.textarea = house.find('div', class_='details-item').contents[3].textfloor = house.find('div', class_='details-item').contents[5].textyear = house.find('div', class_='details-item').contents[7].textbroker = house.find('span', class_='brokername').textbroker = broker[1:]address = house.find('span', class_='comm-address').text.strip()address = address.replace('\xa0\xa0\n                  ', ' ')tag_list = house.find_all('span', class_='item-tags')tags = [i.text for i in tag_list]temp = [name, price, price_area, no_room, area,floor, year, broker, address, tags]print(temp)# 写入表格(test.csv)w.writerow(temp)

附录一,请求头的查找:

打开浏览器到你要爬取的网站 -->  右键选择查看元素 --> 点击网络(如图一)-->

重新载入当前页面 -- > 右键点击开发者工具栏--> 点击原始头(如图二)就可以看到请求头

图一

图二

 

参考文献:python网络爬虫从入门到实践 唐松等 【2017.9】

更多推荐

爬取安居客上住房信息的简单爬虫 ,并存储为表格文件

本文发布于:2024-02-06 04:19:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1746357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爬虫   表格   住房   简单   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!