四.BeautifulSoup爬取51job任意职位并且存储到mongodb

#coding:utf-8
import requests
import re
import urllib2
from pymongo import MongoClient
from bs4 import BeautifulSoup
#抓取51job相关职位信息

def get_url():
    #连接mongo数据库
    cn=MongoClient(host='127.0.0.1',port=27017)
    db=cn.job
    table=db.autoTable

    #初始化数据
    rel=True
    line=1
    url_name=urllib2.quote(name.encode('utf-8'))
    header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
    while rel:
        url='http://search.51job.com/list/020000,000000,0000,00,9,99,{},2,{}.html'.format(url_name.replace('%','%25'),line)
        rq=requests.get(url,headers=header)
        bs=BeautifulSoup(rq.content,'html.parser')
        page=bs.find('span',class_="td").string
        page_num=re.search('\d{1,}',page).group()
        if line<=int(page_num):
            print u'正在抓取%s页面信息'%line
        #   print bs.prettify(encoding='gbk')
            div=bs.find_all('div',class_="el")
            for data in div:
                if data.find_all('p', class_="t1 "):
                    jobdic={}
                    #正则获取需要的信息
                    jobdic['job_name']=data.p.span.a.attrs['title']
                    jobdic['job_request_href']=data.p.span.a.attrs['href']
                    jobdic['job_company']=data.find('span',class_="t2").a.attrs['title']
                    jobdic['job_place']=data.find('span',class_="t3").string
                    jobdic['job_money']=data.find('span',class_="t4").string
                    jobdic['job_pushtime']=data.find('span',class_="t5").string
                    #存储数据
                    table.save(jobdic)
            line+=1
        else:
            rel=False
if __name__=='__main__':
    name=u'自动化测试工程师'
    get_url()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值