目标网站:51job
爬取引入库:
from bs4 import BeautifulSoup #网页解析,获取数据 import re #正则表达式,进行文字匹配 import urllib.request,urllib.error #制定URL,获取网页数据 import sqlite3 #进行SQLite数据库操作 import json
爬取网页:
#模拟浏览器头部信息,向服务器发送消息
head = {"User-Agent": "****"}
#用户代理,表示告诉豆瓣服务器,我们是什么类型的机器、浏览器(本质上告诉浏览器,我们可以接收什么水平的文件内容)
req = urllib.request.Request(url,headers=head)
html = ""
try:
res = urllib.request.urlopen(req)
html = res.read().decode("gbk")
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
解析网页:
爬取下来我们发现网站招聘信息的目录中的数据以json储存在这当中


