Python爬虫(十五)_案例:使用bs4的爬虫
本章将从Python案例讲起:所使用bs4做一个简单的爬虫案例,更多内容请参考:Python学习指南
案例:使用BeautifulSoup的爬虫
我们已腾讯社招页面来做演示:http://hr.tencent.com/position.php?&start=10#a

使用BeautifulSoup4解析器,将招聘网页上的职位名称、职位类别、招聘人数、工作地点、时间、以及每个职位详情的点击链接存储出来。
#-*- coding:utf-8 -*-
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
print(result)
items = []
for site in result:
item = {}
name = site.select('td a')[0].get_text()
dataLink = 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['datailLink'] = url + dataLink
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爬虫(十五)_案例:使用bs4的爬虫的更多相关文章
- [b0030] python 归纳 (十五)_多进程使用Pool
1 usePool.py #coding: utf-8 """ 学习进程池使用 multiprocessing.Pool 总结: 1. Pool 池用于处理 多进程,并不 ...
- Python笔记(十五)_异常处理
try-except语句 try: 被检测代码 except Exception [as reason]: 出现异常后的处理代码 例: try: sum = 1+' f=open('未定义文件.txt ...
- Python进阶(十五)----面向对象之~继承(单继承,多继承MRO算法)
Python进阶(十五)----面向对象之~继承 一丶面向对象的三大特性:封装,继承,多态 二丶什么是继承 # 什么是继承 # b 继承 a ,b是a的子类 派生类 , a是b的超类 基类 父类 # ...
- 初学 Python(十五)——装饰器
初学 Python(十五)--装饰器 初学 Python,主要整理一些学习到的知识点,这次是生成器. #-*- coding:utf-8 -*- import functools def curren ...
- Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块
Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fab ...
- 孤荷凌寒自学python第二十五天初识python的time模块
孤荷凌寒自学python第二十五天python的time模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 通过对time模块添加引用,就可以使用python的time模块来进行相关的时间操 ...
- 孤荷凌寒自学python第十五天python循环控制语句
孤荷凌寒自学python第十五天python循环控制语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) python中只有两种循环控制语句 一.while循环 while 条件判断式 1: ...
- Python爬虫(十八)_多线程糗事百科案例
多线程糗事百科案例 案例要求参考上一个糗事百科单进程案例:http://www.cnblogs.com/miqi1992/p/8081929.html Queue(队列对象) Queue是python ...
- Python爬虫(十九)_动态HTML介绍
JavaScript JavaScript是网络上最常用也是支持者对多的客户端脚本语言.它可以收集用户的跟踪数据,不需要重载页面直接提交表单,在页面嵌入多媒体文件,甚至运行网页游戏. 我们可以在网页源 ...
随机推荐
- C# group 子句
group 子句返回一个 IGrouping<TKey,TElement> 对象序列,这些对象包含零个或更多与该组的键值匹配的项. 例如,可以按照每个字符串中的第一个字母对字符串序列进行分 ...
- ⑿bootstrap组件 缩略图 警告框 进度条 基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- HTML学习笔记 基础标签及css引用案例 第一节 (原创)参考使用表
<!DOCTYPE html><!--头文件 不是标签 也没有结束,这是声明该文件为HTML5--><html lang="en"><!- ...
- 从一个实例谈谈postgresql索引锁
最近客户在使用我司开发的数据库时,报告了如下问题(也不能算是问题,就是疑惑吧),环境如下: OS : Red Hat Enterprise Linux Server release 6.7 (Sant ...
- JavaScript核心参考
Array 方法 concat() 把元素衔接到数组中. every() 测试断言函数是否对每个数组元素都为真. filter() 返回满足断言函数的数组元素. forEach() 为数组的每一个元素 ...
- [array] leetCode-4-Median of Two Sorted Arrays-Hard
leetCode-4-Median of Two Sorted Arrays-Hard descrition There are two sorted arrays nums1 and nums2 o ...
- How to set up Dynamics CRM 2011 development environment
Recently I have been starting to learn Microsoft Dynamics CRM 2011 about implement plugin and workfl ...
- [Intel Edison开发板] 06、Edison开发在linux中烧写、配置、搭建开发环境
1.前言 linux上烧写.配置.搭建Edison环境,千万不要用默认的setup tool for ubuntu!!! (即使,你用的就是ubuntu) 因为,其默认的工具会从一个坏链接下载配置文件 ...
- C#调用DLL文件时参数对应表
Wtypes.h中的非托管类型 非托管 C语言类型 托管类名 说明HANDLE void* Syste ...
- TCollector
TCollector tcollector is a client-side process that gathers data from local collectors and pushes th ...