大家好!在这里给大家交流一下小编总结得python超简单爬虫教程,废话不多说,直接上代码
完整代码如下:
import request
import re
import xlwt
#获取源码
def get_content(page):
url = 'https://www.zhipin.com/c101010100-p100109/?page='+str(page)+'&ka=page-'+str(page)+''
headers = {
"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6788.400 QQBrowser/10.3.2727.400"
}
req = request.get(url,headers=headers)
req.encoding = 'utf-8'
html = req.text
return html
#筛选数据 获取满足正则表达式所爬去的目标
def get(html):
reg = re.compile(r'class="job-tab".*?(.*?).*?(.*?).*?target="_blank">(.*?)',re.S)
item = re.findall(reg,html)
return items
datalist = []
def saveDataToDatalist():
for j in range(1,6)
print('正在爬取第'+str(j)+'页数据内容。。。')
html get_content(j)
for i in get(html)
data = []
for j in range(0,5):
data.append(i[j])
datalist.append(data)
return
#创建方法将datalist内容写入excel文件
def saveDataToExcel(savePath):
book = xlwt.Workbook(encoding='utf-8',style_compression=0)
sheet = book.add_sheet('boss',cell_overwrite_ok=Ture)
col = ('职位','薪资','公司名称')
for i in range(0,5)
sheet.wirte(0,i,col[i])
for i in range(0,len(0,len(datalist)):
data = datalist[i]
for j in range(0,5):
sheet.wirte(i+1,j,data[j])
book.save(savePath)
saveDataToDatalist()
saveDataToExcel('boss.xls')
就这样了,都看到这里了,点赞关注走一走吧,谢谢大家!