爬虫2

作者: 冬gua | 来源:发表于2018-03-21 21:46 被阅读0次

爬虫之 beautifulsoup

Beautiful Soup 3目前已经停止开发,推荐现在的项目使用Beautiful Soup 

利用beautiful 爬取

import requests

from bs4 import BeautifulSoup

import json

'''发送请求,获取响应的内容'''

headers = {

    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)      Chrome/63.0.3239.132 Safari/537.36',

}

base_url = 'https://hr.tencent.com/position.php'

keywords = input('输入职位:')

begin_page = int(input('起始页:'))

end_page = int(input('结束页:'))

job_list = []

for page in range(begin_page, end_page + 1):

    params = {

        'keywords': keywords,

        'start': (page - 1) * 10

    }

    print('%s爬取中...' % page)

    response1 = requests.get(url=base_url, params=params,headers=headers)

    content = response1.content

    # with open('./tencent-%s.html'%page, 'wb') as file:

    #     file.write(content)

    content = content.decode('utf-8')

    '''数据提取'''

    bs = BeautifulSoup(content,'lxml')

    # tr_list = bs.select('tr[class="odd"],tr[class="even"]')

    tr_list = bs.find_all(name='tr',attrs={'class':['even','odd']})

    for tr in tr_list:

        job={}

        job['job_name'] = tr.a.text.strip()

        job['job_href'] = tr.a['href']

        job['job_type'] = tr.find_all('td')[1].text.strip()

        job['job_person'] = tr.find_all('td')[2].text.strip()

        job['job_address'] = tr.find_all('td')[3].text.strip()

        job['job_time'] = tr.find_all('td')[4].text.strip()

        job_list.append(job)

#转成json            ensure_asci=False----默认是True,改成False,才能显示中文, ensure_ascii=False 来禁用ascii编码

#dump 和 dumps两种写法,

# job_json_string = json.dumps(job_list,ensure_ascii=False)

# with open('./tencent.json', 'w',encoding='utf-8') as file:

#     file.write(job_json_string)

json.dump(job_list,open('./tencent.json', 'w',encoding='utf-8'),ensure_ascii=False)

相关文章

  • python-爬虫基础(慕课网)

    二.爬虫简介以及爬虫的技术价值 2-1:爬虫是什么? 2-2:爬虫技术的价值? 三.简单爬虫架构 3-1:简单爬虫...

  • Python网络爬虫

    Python开发简单爬虫(Python2.X版本,Eclipse工具) 一、爬虫介绍 爬虫调度端:启动、停止爬虫,...

  • 2018-05-13

    Scrapy爬虫 1.新建爬虫工程 scrapy startproject Spider(项目名字) 2.创建爬虫...

  • 爬虫01:概述

    爬虫概述 1.目录清单 爬虫简介 通用爬虫和聚焦爬虫 网络请求那些事儿 网络数据抓包分析 2.章节内容 2.1爬虫...

  • Python爬虫简述系列之一

    1,简单分类 根据使用场景,网络爬虫可分为 通用爬虫 和 聚焦爬虫 两种. 2,通用爬虫 通用网络爬虫 是 捜索引...

  • 6张脑图系统讲透python爬虫和数据分析、数据挖掘

    1、python爬虫:比较详细介绍了爬虫所需要具备的库、工具、爬虫基础知识 2、python爬虫流程 3、pyth...

  • Python爬虫入门(urllib+Beautifulsoup)

    Python爬虫入门(urllib+Beautifulsoup) 本文包括:1、爬虫简单介绍2、爬虫架构三大模块3...

  • 深度爬虫

    scrapy深度爬虫 1.深度爬虫概述2.scrapy Spider实现的什么爬虫3.scrapy CrawlSp...

  • 爬虫——Web Scraper

    1.认识爬虫 2.利用Excel抓取数据 3.爬虫入门 4.爬虫进阶 5.反爬虫及高阶玩法 6.制作新爬虫步骤 7...

  • python爬虫01

    爬虫概述 1. 目录清单 爬虫简介 通用爬虫和聚焦爬虫 网络请求那些事儿 网络数据抓包分析 2. 章节内容 2.1...

网友评论

      本文标题:爬虫2

      本文链接:https://www.haomeiwen.com/subject/yfinqftx.html