欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python爬虫爬取58同城北京品牌公寓租房信息

发布时间:2024/5/14 python 57 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python爬虫爬取58同城北京品牌公寓租房信息 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
from bs4 import BeautifulSoup from urllib.parse import urljoin import requests import csvurl = "http://bj.58.com/pinpaigongyu/pn/{page}/?minprice=2000_4000"#已完成的页数序号,初时为0 page = 0csv_file = open("rent.csv","wb") csv_writer = csv.writer(csv_file, delimiter=',')while True:page += 1print ("fetch: ", url.format(page=page))response = requests.get(url.format(page=page))html = BeautifulSoup(response.text,"lxml")house_list = html.select(".list > li")# 循环在读不到新的房源时结束if not house_list:if sign == 3:print(" 已经度曲完毕")breakelse:print(" 出现获取不到列表,再次尝试")sign += 1time.sleep(random.randint(1,3))continuesign = 0for house in house_list:house_title =str(house.select("h2")[0].string)house_url = urljoin(url, house.select("a")[0]["href"])house_info_list = house_title.split()# 如果第二列是公寓名则取第一列作为地址if "公寓" in house_info_list[1] or "青年社区" in house_info_list[1]:house_location = house_info_list[0].split("]")[1]else:house_location = house_info_list[1]house_money = house.select(".money")[0].select("b")[0].string.encode("utf8")csv_writer.writerow([house_title, house_location, house_money, house_url])csv_file.close()

总结

以上是生活随笔为你收集整理的python爬虫爬取58同城北京品牌公寓租房信息的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。