Python2 Python3 爬取趕集網租房信息,帶源碼分析

*之前偶然看了某個騰訊公開課的視頻,寫的爬取趕集網的租房信息,這幾天突然想起來,于是自己分析了一下趕集網的信息,然后自己寫了一遍,寫完又用用Python3重寫了一遍.之中也遇見了少許的坑.記一下.算是一個總結.*

python2 爬取趕集網租房信息與網站分析

  1. 分析目標網站url
  2. 尋找目標標簽
  3. 獲取,并寫入csv文件
#-*- coding: utf-8 -*-
from bs4 import BeautifulSoup
from urlparse import urljoin
import requests
import csvURL = 'http://jn.ganji.com/fang1/o{page}p{price}/'
# 首先最基本的是 jn,ganji.com/fang1 其中jn為濟南,也就是我的城市,默認登錄后為此
# 而fang1 位租房信息 fang5 為二手房信息,zhaopin 為招聘模塊等,我們這次只查找fang1
# 不過這個鏈接還可以更復雜 
#比如http://jn.ganji.com/fang1/tianqiao/h1o1p1/ 或者
#  http://jn.ganji.com/fang1/tianqiao/b1000e1577/
#    其中h:房型,o頁面,p價格區間,其中h,p后的數字與網站相應菜單的排列順序相對應 
# 而s與e則為對應的自己輸入的價格區間
#                          h: house o:page p:price
# jn jinan  fang1 zufang tiaoqiao:tianqiaoqu b:begin 1000  e:end start 1755ADDR = 'http://bj.ganji.com/'
start_page =1
end_page = 5
price =1# 注意wb格式打開寫入可能會導致csv文件每次寫入前面多一個空格
# 對此你可以參考這篇文章:http://blog.csdn.net/pfm685757/article/details/47806469
with open('info.csv','wb') as f :csv_writer = csv.writer(f,delimiter=',')print 'starting'while start_page<end_page:start_page+=1# 通過分析標簽可知我們要獲取的標簽信息必須要通過多個class確認才能保證唯一性# 之后是獲取信息的具體設置print 'get{0}'.format(URL.format(page = start_page,price=price))response = requests.get(URL.format(page = start_page,price=price))html=BeautifulSoup(response.text,'html.parser')house_list = html.select('.f-list > .f-list-item > .f-list-item-wrap')#check house_listif not house_list:print 'No house_list'breakfor house in house_list:house_title = house.select('.title > a')[0].string.encode('utf-8')house_addr = house.select('.address > .area > a')[-1].string.encode('utf-8')house_price = house.select('.info > .price > .num')[0].string.encode('utf-8')house_url = urljoin(ADDR,house.select('.title > a ')[0]['href'])# 寫入csv文件csv_writer.writerow([house_title,house_addr,house_price,house_url])print 'ending'

Python3 爬取趕集網i租房信息

要注意的點

  1. urlparse.urljoin 改為urllib.urlparse.urljoin
# python2
from urlparse import urljoin
# Python3
from urllib.parse  import urljoin
  1. Python3中csv對bytes和str兩種類型進行了嚴格區分,open的寫入格式應該進行改變wb->w
  2. 設置utf8編碼格式
with open('info.csv','w',encoding='utf8') as f :csv_writer = csv.writer(f,delimiter=',')

完整代碼如下

#-*- coding: utf-8 -*-
from bs4 import BeautifulSoup
from urllib.parse  import urljoin
import requests
import csvURL = 'http://jn.ganji.com/fang1/o{page}p{price}/'
#                                h: house o:page p:price
#  http://jn.ganji.com/fang1/tianqiao/b1000e1577/
# jn jinan  fang1 zufang tiaoqiao:tianqiaoqu b:begin 1000  e:end start 1755
# fang5 為二手房 zhipin 為 招聘 趕集網的url劃分的都很簡單,時間充足完全可以獲取非常多的信息ADDR = 'http://bj.ganji.com/'
start_page =1
end_page = 5price =1'''
URL = 'http://jn.ganji.com/fang1/h{huxing}o{page}b{beginPrice}e{endPrice}/'
# 選擇戶型為h1-h5
# 輸入價位為 begin or end
price='b1000e2000'# 戶型為'''
# 默認為utf8打開,否則會以默認編碼GBK寫入
with open('info.csv','w',encoding='utf8') as f :csv_writer = csv.writer(f,delimiter=',')print('starting')while start_page<end_page:start_page+=1print('get{0}'.format(URL.format(page = start_page,price=price)))response = requests.get(URL.format(page = start_page,price=price))html=BeautifulSoup(response.text,'html.parser')house_list = html.select('.f-list > .f-list-item > .f-list-item-wrap')#check house_listif not house_list:print('No house_list')breakfor house in house_list:house_title = house.select('.title > a')[0].stringhouse_addr = house.select('.address > .area > a')[-1].stringhouse_price = house.select('.info > .price > .num')[0].stringhouse_url = urljoin(ADDR, house.select('.title > a ')[0]['href'])csv_writer.writerow([house_title,house_addr,house_price,house_url])print('ending')

最后的csv文件展示一下:
趕集網租房信息 csv文件 網絡爬蟲

轉載于:https://www.cnblogs.com/fonttian/p/9162844.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/252827.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/252827.shtml
英文地址,請注明出處:http://en.pswp.cn/news/252827.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

紅外熱成像技術原理

目前&#xff0c;新的熱成像儀主要采用非致冷焦平面陣列技術&#xff0c;集成數萬個乃至數十萬個信號放大器&#xff0c;將芯片置于光學系統的焦平面上&#xff0c;無須光機掃描系統而取得目標的全景圖像&#xff0c;從而大大提高了靈敏度和熱分辨率&#xff0c;并進一步地提高…

網站中公用頭部與尾部

一、html 1. <iframe src"1.html" frameborder"0"></iframe> 2. <embed src"1.html"/> 二、寫公用的js 文件&#xff0c;js中寫字divde符串&#xff0c;然后在需要的頁面適當位置引入公用的js. 三、ajax動態拉取填充 四、后端…

基于webpack的PC端和mobile端開發以及生產環境的搭建

我們用webpack做單頁面應用開發應該嘗試過很多次了&#xff0c;如果在同一個項目需要支持PC端和移動端&#xff0c;做成多頁面應用&#xff0c;開發時能根據請求的終端返回不同的內容&#xff0c;應該怎么做呢&#xff1f;以下描述的是我嘗試的一種方案&#xff0c;并且以vue-c…

深度學習簡介(一)——卷積神經網絡

本文的主要目的&#xff0c;是簡單介紹時下流行的深度學習算法的基礎知識&#xff0c;本人也看過許多其他教程&#xff0c;感覺其中大部分講的還是太過深奧&#xff0c;于是便有了寫一篇科普文的想法。博主也是現學現賣&#xff0c;文中如有不當之處&#xff0c;請各位指出&…

node+mongoose使用例子

https://github.com/Aquarius1993/nodeNotes 功能 1. 注冊 2. 登錄 3. 修改密碼 4. 修改頭像 5. 獲取用戶筆記 6. 添加&#xff0c;刪除,更新筆記安裝部署 1. 安裝 Node.js MongoDB 2. npm install 3. 啟動 MongoDB 4. npm start 5. visit http://localhost:8899 https://git…

FastJson的常用操作

轉載&#xff1a;https://www.cnblogs.com/Ming8006/p/6944954.html 常用操作包括以下內容&#xff1a; 對象與&#xff08;JsonObject或JsonArray&#xff09;與String的互換String轉換為&#xff08;JsonObject或JsonArray&#xff09;如何保持順序Eval的使用值為null的&…

Jenkins持續集成 之 Jenkins安裝

一、安裝JDK與TOMCAT8 參考地址&#xff1a; http://ibm.chick.blog.163.com/blog/static/144201610201652811537410/ 二、下載Jenkins安裝包 wget http://mirrors.jenkins.io/war/latest/jenkins.war 三、把jenkins.war放到TOMCAT下的webapps 四、啟動tomcat并訪問 http://10.…

夜視模式,多少猥瑣相機假汝之名

一般相機加裝一個紅外濾光片&#xff0c;就是一個可以用來干猥瑣事情的相機了。現在有相機直接把這個功能整合了&#xff0c;它的這個功能叫紅外夜視模式&#xff0c;可以拍下黑暗中的物體&#xff0c;當然&#xff0c;也可以透視紡織品和一些其他材質的衣物。這部相機具有500萬…

vue-router.esm.js?fe87:16 [vue-router] Route with name 'page' does not exist

本文地址&#xff1a;http://www.cnblogs.com/veinyin/p/7910525.html 我的路由配置 1 { 2 path: /page, 3 name: page, 4 component: page 5 } 我的 router-link <router-link :to"{name:page,params:{trans:ahahahaha}}">page</route…

Java中設計模式之單例設計模式-1

單例作用 1 節省內存2 可以避免多種狀態導致狀態沖突單例的創建步驟 1 私有化構造方法2 私有化聲明的屬性3 getInstance4 方法需要靜態單例分類 1.懶漢式 2.餓漢式 兩種單例區別&#xff1a; 餓漢式 線程安全的 懶漢式 線程不安全的 餓漢式&#xff1a; package 設計模式之單…

Vue 父子組件間的通信

前言 在 Vue 項目中父子組件的通信是非常常見的&#xff0c;最近做項目的時候發現對這方面的知識還不怎么熟練&#xff0c;在這邊做一下筆記&#xff0c;系統學習一下吧。 1 父組件傳值給子組件 1.1 傳值寫法 父組件傳值給子組件&#xff0c;這個就比較常見了&#xff0c;直接用…

夜視儀

國民兵所用F-16的駕駛艙&#xff0c;機員配有夜視鏡以備觀察領空的客機或飛行物體使用夜視鏡時看到的影像夜視儀〈night vision device&#xff0c;簡稱NVD〉&#xff0c;也稱夜視鏡&#xff0c;是透過光學科技強化黑暗中光源的科技裝置&#xff0c;通常用于軍事用途。目前在民…

【物理/數學】—— 概念的理解 moment、momentum

moment&#xff1a;矩&#xff0c;momentum&#xff1a;[物] 動量&#xff1b;動力&#xff1b;沖力&#xff1b; 數學意義上的 moment&#xff08;矩&#xff09;概念其實源自于物理范疇。首先我們來介紹物理學意義上的矩&#xff08;Momentum&#xff09;的概念。 1. 物理學意…

JAVA多線程實現的三種方式

JAVA多線程實現方式主要有三種&#xff1a;繼承Thread類、實現Runnable接口、使用ExecutorService、Callable、Future實現有返回結果的多線程。 其中前兩種方式線程執行完后都沒有返回值&#xff0c;只有最后一種是帶返回值的。 前兩種省略&#xff0c;著重看看第三種&#xff…

2springboot:快速創建springboot項目

使用IDEA快速創建springboot項目流程&#xff1a; 創建新的項目選擇 項目的命名以及包名 需要什么包就導入什么包 進行測試的單元 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId>&…

如何做好渠道銷售?

作者&#xff1a;路人李志強鏈接&#xff1a;https://www.zhihu.com/question/19689177/answer/28508799來源&#xff1a;知乎著作權歸作者所有。商業轉載請聯系作者獲得授權&#xff0c;非商業轉載請注明出處。作為一名一線品牌的渠道銷售&#xff0c;如何讓代理商放棄其他小品…

2017浙江省賽 B - Problem Preparation ZOJ - 3959

地址&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode3959 題目&#xff1a; Its time to prepare the problems for the 14th Zhejiang Provincial Collegiate Programming Contest! Almost all members of Zhejiang University programming conte…

jQuery導入代碼片段并綁定事件

a.html <div><button class"button" >點我達</button> </div> b.html <html> <head><script type"text/javascript" src"./js/jquery-1.0.1.js"></script><meta http-equiv"Content-Ty…

驗證碼識別

基于Keras&#xff0c;識別簡單的4位字符的驗證碼。 https://github.com/ypwhs/captcha_break

LinCode落單的數

easy 落單的數 查看執行結果 60% 通過給出2*n 1 個的數字&#xff0c;除當中一個數字之外其它每一個數字均出現兩次。找到這個數字。 您在真實的面試中是否遇到過這個題&#xff1f; Yes例子 給出 [1,2,2,1,3,4,3]。返回 4 挑戰 一次遍歷&#xff0c;常數級的額外空間復雜度…