python爬虫爬取58同城北京品牌公寓租房信息
生活随笔
收集整理的這篇文章主要介紹了
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同城北京品牌公寓租房信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenGL反射光
- 下一篇: 2022年中科大细胞生物学实验原理往年题