Python爬虫简单实战:抓取小猪短租西安市前五页民房数据
2017-06-02 17:30
731 查看
from bs4 import BeautifulSoup import requests urls=[] #url列表 for page in range(1,6,1): #前五页 page_url='http://xa.xiaozhu.com/search-duanzufang-p{}-0/'.format(page) home_page=requests.get(page_url) soup_home=BeautifulSoup(home_page.text,'lxml') detail_urls=soup_home.select('a.resule_img_a') #取得每个民房的详情页url for detail_url in detail_urls: detail_url=detail_url.get('href') urls.append(detail_url) for url in urls: web=requests.get(url) soup=BeautifulSoup(web.text,'lxml') #select方法获得的是列表,列表中的第一个元素(唯一一个元素)即是要找的信息 用索引取出后在使用方法 #因为 beautifulsoup 的方法并不能列表类型的元素使用 titles=soup.select('div.pho_info > h4 > em')[0].text #the difference between text() and get_text() adds=soup.select('div.pho_info > p')[0].get('title') prices=soup.select('div.day_l > span')[0].text pics=soup.select('div.pho_show_big > div:nth-of-type(2) > img')[0].get('src') member_pics=soup.select('div.member_pic > a > img')[0].get('src') member_names=soup.select('div.w_240 > h6 > a')[0].text gender_ico=soup.select('div.member_pic > div')[0].get('class')[0] if gender_ico=='member_ico1': #由图标名称来判断房东性别 gender='female' else:gender='male' data={ 'title':titles, 'add':adds, 'prices':prices, 'pic':pics, 'member_pic':member_pics, 'member_name':member_names, } print(data)
小猪短租西安市url:http://xa.xiaozhu.com/search-duanzufang-p1-0/
详情页跳转url:
性别图标区分:
相关文章推荐
- python 爬虫 实战(一) —— 抓取学校开课数据
- Python爬虫框架Scrapy实战之抓取户外数据
- python爬虫---post抓取查询数据简单说明
- 实践项目十:爬取百度百科Python词条相关1000个页面数据(慕课简单爬虫实战)
- Python数据抓取(2) —简单网络爬虫的撰写
- 简单的python爬虫抓取图片实例
- python 抓取腾讯微博数据并做简单的分析
- Python实现抓取页面上链接的简单爬虫分享
- 使用Python编写简单网络爬虫抓取视频下载资源
- 【Python数据分析】简单爬虫 爬取知乎神回复
- Python爬虫框架Scrapy 学习笔记 10.2 -------【实战】 抓取天猫某网店所有宝贝详情
- Python爬虫框架Scrapy 学习笔记 10.3 -------【实战】 抓取天猫某网店所有宝贝详情
- Python爬虫处理抓取数据中文乱码问题
- Python爬虫框架Scrapy 学习笔记 10.1 -------【实战】 抓取天猫某网店所有宝贝详情
- Python 网络爬虫5 ---- 第一次实现抓取数据并且存放到mysql数据库中
- 使用python 编写 抓取内涵段子动态图的简单爬虫
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍
- python采用requests库模拟登录和抓取数据的简单示例
- 简单的抓取淘宝图片的Python爬虫
- [Python学习] 简单网络爬虫抓取博客文章及思想介绍