python 案例:使用BeautifuSoup4的爬虫
我们以腾讯社招页面来做演示:http://hr.tencent.com/position.php?&start=10#a

使用BeautifuSoup4解析器,将招聘网页上的职位名称、职位类别、招聘人数、工作地点、发布时间,以及每个职位详情的点击链接存储出来。

# bs4_tencent.py
from bs4 import BeautifulSoup
import urllib2
import urllib
import json # 使用了json格式存储
def tencent():
url = 'http://hr.tencent.com/'
request = urllib2.Request(url + 'position.php?&start=10#a')
response =urllib2.urlopen(request)
resHtml = response.read()
output =open('tencent.json','w')
html = BeautifulSoup(resHtml,'lxml')
# 创建CSS选择器
result = html.select('tr[class="even"]')
result2 = html.select('tr[class="odd"]')
result += result2
items = []
for site in result:
item = {}
name = site.select('td a')[0].get_text()
detailLink = site.select('td a')[0].attrs['href']
catalog = site.select('td')[1].get_text()
recruitNumber = site.select('td')[2].get_text()
workLocation = site.select('td')[3].get_text()
publishTime = site.select('td')[4].get_text()
item['name'] = name
item['detailLink'] = url + detailLink
item['catalog'] = catalog
item['recruitNumber'] = recruitNumber
item['publishTime'] = publishTime
items.append(item)
# 禁用ascii编码,按utf-8编码
line = json.dumps(items,ensure_ascii=False)
output.write(line.encode('utf-8'))
output.close()
if __name__ == "__main__":
tencent()
python 案例:使用BeautifuSoup4的爬虫的更多相关文章
- 2.6. 案例:使用BeautifuSoup4的爬虫
案例:使用BeautifuSoup4的爬虫 我们以腾讯社招页面来做演示:http://hr.tencent.com/position.php?&start=10#a 使用BeautifuSou ...
- python Cmd实例之网络爬虫应用
python Cmd实例之网络爬虫应用 标签(空格分隔): python Cmd 爬虫 废话少说,直接上代码 # encoding=utf-8 import os import multiproces ...
- 获取字段唯一值工具- -ArcPy和Python案例学习笔记
获取字段唯一值工具- -ArcPy和Python案例学习笔记 目的:获取某一字段的唯一值,可以作为工具使用,也可以作为函数调用 联系方式:谢老师,135-4855-4328,xiexiaokui# ...
- 使用python做最简单的爬虫
使用python做最简单的爬虫 --之心 #第一种方法import urllib2 #将urllib2库引用进来response=urllib2.urlopen("http://www.ba ...
- 14.python案例:爬取电影天堂中所有电视剧信息
1.python案例:爬取电影天堂中所有电视剧信息 #!/usr/bin/env python3 # -*- coding: UTF-8 -*- '''======================== ...
- 第7.27节 Python案例详解: @property装饰器定义属性访问方法getter、setter、deleter
上节详细介绍了利用@property装饰器定义属性的语法,本节通过具体案例来进一步说明. 一. 案例说明 本节的案例是定义Rectangle(长方形)类,为了说明问题,除构造函数外,其他方法都只 ...
- 第7.25节 Python案例详解:使用property函数定义与实例变量同名的属性会怎样?
第7.25节 Python案例详解:使用property函数定义与实例变量同名的属性会怎样? 一. 案例说明 我们上节提到了,使用property函数定义的属性不要与类内已经定义的普通实例变量重 ...
- 第7.24节 Python案例详解:使用property函数定义属性简化属性访问代码实现
第7.24节 Python案例详解:使用property函数定义属性简化属性访问代码实现 一. 案例说明 本节将通过一个案例介绍怎么使用property定义快捷的属性访问.案例中使用Rectan ...
- 案例:使用BeautifuSoup4的爬虫
使用BeautifuSoup4解析器,将招聘网页上的招聘单位名称存储出来.其他信息可类似爬取即可 # -*- coding:utf-8 -*- from bs4 import BeautifulSou ...
随机推荐
- Ext js 应用例子
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 2017年最全的30个Android面试题,你将如何回答?
百度首页 登录 2017年最全的30个Android面试题,你将如何回答? 机翼技术 百家号 03-10 02:32 “三金四银”又是一年一度的跳槽季,相信有不少Android程序员开始摩拳擦掌蠢蠢欲 ...
- [Android UI] Service里面启动Activity和Alertdialog
启动Activity源码:(记得要加上Intent.FLAG_ACTIVITY_NEW_TASK) Intent intent = new Intent(); intent.setFlags(Inte ...
- mysql 初始化报错 /usr/local/mysql/bin/mysqld:error while loading shared libraries :libaio.so.1
安装mysql在初始化的时候,出现/usr/local/mysql/bin/mysqld:error while loading shared libraries:libaio.so.1 :canno ...
- iOS:Xcode7下创建 .a静态库 和 .framework静态库
Xcode7 中创建静态库:.a 和 .framework 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的分类 根据源代码的公开情况,库可以分为2种类型 (1)开 ...
- iOS: iOS9 beta 请求出现App Transport Security has blocked a cleartext HTTP (http://)
错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...
- Vue基础知识总结(二)
一.解决网速慢的时候用户看到花括号标记 (1)v-cloak,防止闪烁,通常用于比较大的选择器上. 给元素添加属性v-cloak,然后style里面:[v-cloak]{display:none;} ...
- FIS 配置小诀窍
之前用 FIS 的时候,发现配置 roadmap 的时候出现了非常诡异的现象:命令行使用 -o 参数,配置文件里对 html 不使用优化,导致 uglify 了 js 文件后,不会修改 html 中对 ...
- Selenium webdriver Java 开始
最早接触的selenium是 selenium IDE,当时是为了准备论文.为了用IDE还下载了Firefox浏览器.后来接触过两个项目都需要selenium,一个采用selenium webdirv ...
- 小程序app is not defined
错误记录: 小程序丨 报错:app is not defined; 解决方案: Js头部添加:var app = getApp(); 返回按钮: wx.navigateBack(); 转发 ...