放养的小爬虫--拉钩网半智能整站小爬虫
我是一只放养的小爬虫--拉钩网半智能整站小爬虫
笔者声明:只用于学习交流,不用于其他途径。源代码已上传github。githu地址:https://github.com/Erma-Wang/Spider
Python写爬虫的感觉那叫一个爽!100行代码不到,爬取整站,貌似这样下去拉钩还不加强服务器么?下面看看半智能的效果,程序员嘛。。。不做外壳了。。。我只是一只放养的小爬虫,拉钩看到就说对不起啰!!。哈哈,下面看图:
输入命令后,小爬虫开始工作了!
爬去结束后小爬虫自动生成了一个XLS文件,一般的excel就能打开了
最后看看成果:
好了,效果看完了,看看小爬虫怎么制造出来的吧。。。
其中也没有什么难点的技术,不过pandas很值得学习,给个学习链接http://pandas.pydata.org,挺不错的。。下面贴一下代码吧。。。github上面也有。
# -*- coding:utf-8 -*-
import re,json
from urllib import request
from pandas import DataFrame,Series
import pandas as pd
__author__ = "放养的小爬虫"
# 处理字符串的函数
def ProcessingString(string):
string = string.encode('utf-8')
string = str(string).replace(r'\x','%').replace(r"'","")
string = re.sub('^b','',string)
return string
# 计算总共页数
def SearchPageCount(position, city):
i = 0
type = 'true'
url = 'http://www.lagou.com/jobs/positionAjax.json?city='+city+'&first='+type+'&kd='+position+'&pn='+str(i+1)
with request.urlopen(url) as f:
data = f.read()
count = int(json.loads(str(data,encoding='utf-8',errors='ignore'))["content"]["totalPageCount"])
totalCount = int(json.loads(str(data,encoding='utf-8',errors='ignore'))["content"]["totalCount"])
print('本次搜索到%d个职位'%totalCount)
return count
def LaGouSpiderWithKeyWord(position, city):
positionTemp = ProcessingString(position)
cityTemp = ProcessingString(city)
# 获取总共页数
pageCount = SearchPageCount(positionTemp,cityTemp)
for i in range(0,pageCount):
if i ==0 :
type='true'
else:
type='false'
url = 'http://www.lagou.com/jobs/positionAjax.json?city='+cityTemp+'&first='+type+'&kd='+positionTemp+'&pn=1'
data = request.urlopen(url).read()
# 读取Json数据
jsondata = json.loads(str(data,encoding='utf-8',errors='ignore'))['content']['result']
for t in list(range(len(jsondata))):
jsondata[t]['companyLabelListTotal']='-'.join(jsondata[t]['companyLabelList'])
jsondata[t].pop('companyLabelList')
if t == 0:
rdata=DataFrame(Series(data=jsondata[t])).T
else:
rdata=pd.concat([rdata,DataFrame(Series(data=jsondata[t])).T])
if i == 0:
totaldata=rdata
else:
totaldata=pd.concat([totaldata,rdata])
print('正在解析第%d页...'%i)
totaldata.to_excel('lagou.xls',sheet_name='sheet1')
if __name__ == "__main__":
position = input('请输入你要爬取的职位')
city = input('请输入你要爬取的城市')
LaGouSpiderWithKeyWord(position, city)
作者声明:只做学习交流,不用于其他途径!!!
Do you want to spend the rest of your life selling sugared water or do you want a chance to change the world?
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?