zoukankan      html  css  js  c++  java
  • [Python] 前程无忧招聘网爬取软件工程职位 网络爬虫 https://www.51job.com

    首先进入该网站的https://www.51job.com/robots.txt页面

    给出提示:

    1 找不到该页       File not found
    2 
    3 您要查看的页已删除,或已改名,或暂时不可用。
    4 
    5 请尝试以下操作:
    6 如果您已经在地址栏中输入该网页的地址,请确认其拼写正确。
    7 打开 www.51job.com 主页,然后查找指向您感兴趣信息的链接。
    8 单击后退按钮,尝试其他链接。

      注:

    • 网络爬虫:自动或人工识别robots.txt,再进行内容爬取
    • 约束性:robots协议建议但非约束性,不遵守可能存在法律风险

    如果一个网站不设置robots协议说明所有内容都可以爬取,所以为可爬取内容。

    源程序如下:

      

     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 # @File  : HtmlParser.py
     4 # @Author: 赵路仓
     5 # @Date  : 2020/2/28
     6 # @Desc  : 前程无忧求职网的爬虫程序
     7 # @Contact : 398333404@qq.com
     8 
     9 from bs4 import BeautifulSoup
    10 import requests
    11 import csv
    12 import re
    13 import io
    14 
    15 # 请求头
    16 head = {
    17     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36'
    18 }
    19 # 链接
    20 url = "https://search.51job.com/list/000000,000000,0000,00,9,99,%25E8%25BD%25AF%25E4%25BB%25B6,2,1.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare="
    21 
    22 
    23 # csv写入表头
    24 def headcsv():
    25     with open('/position.csv', 'w', encoding='utf-8', newline='') as csvfile:
    26         writer = csv.writer(csvfile)
    27         writer.writerow(["职位", "公司", "所在地", "薪酬", "日期", "网址"])
    28 
    29 
    30 # txt写入表头
    31 def headtxt():
    32     ftxt = open('E:/data/position.txt', 'w', encoding='utf-8')
    33     ftxt.write("职位 公司 所在地 薪酬 日期 网址")
    34     ftxt.close()
    35 
    36 
    37 def position(url, head):
    38     # fcsv = open('/position.csv', 'a', encoding='utf-8', newline='')
    39     ftxt = open('E:/data/position.txt', 'a', encoding='utf-8')
    40     try:
    41         r = requests.get(url, headers=head, timeout=3)
    42         # 设置解析编码格式
    43         r.encoding = r.apparent_encoding
    44         print(r.apparent_encoding)
    45         # 打印状态码
    46         print(r.status_code)
    47         # 打印页面代码
    48         # print(r.text)
    49         # print(soup.prettify())
    50         text = r.text
    51         soup = BeautifulSoup(text, 'html.parser')
    52         # 一条招聘信息
    53         item = soup.find_all(class_='el', recursive=True)
    54         num = 0
    55         for i in item:
    56             num += 1
    57             if num > 16:
    58                 itemdetail = i.text.replace(" ", "").replace("
    ", " ").replace("   ", " ").lstrip() + i.find("a").attrs['href']
    59                 print(itemdetail)
    60                 ftxt.write(itemdetail.replace("
    ","")+'
    ')
    61                 print("写入成功")
    62         ftxt.close()
    63     except:
    64         print("爬取职位过程中出错!")
    65 
    66 
    67 def write(url, head):
    68     for i in range(1, 2000):
    69         url = "https://search.51job.com/list/000000,000000,0000,00,9,99,%25E8%25BD%25AF%25E4%25BB%25B6,2,"+str(i)+".html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare="
    70         print(url)
    71         position(url, head)
    72 
    73 
    74 if __name__ == "__main__":
    75     # head()
    76     write(url, head)

    所爬取条目分布为职位 公司 所在地 薪酬 日期 网址 ,保存路径为E:/data/position.txt可自行修改路径或者文件格式。

  • 相关阅读:
    day1 UnicodeEncodeError: 'gbk' codec can't encode character 'xa0' in position 2490: illegal multibyte sequence 错误提示
    day2 Opencv + image
    day1 Opencv安装 python 2.7 (32位)
    2 oracle 实现上下键翻历史命令 rlwrap
    Windows下安装RaibbitMQ
    1. oracle12C的安装
    《万达哲学》------王健林
    MongoDB常用命令
    MongoDB让人恶心的配置
    MongoDB介绍及下载与安装
  • 原文地址:https://www.cnblogs.com/zlc364624/p/12377019.html
Copyright © 2011-2022 走看看