# -*- coding:utf-8 -*-
# 2018年3月24日
# 爬取菜鸟教程最新文章列表并写入Excel中
# 第一步,导入爬虫库,正则库、Excel库(截取文本用的)
from urllib import request
import re
from openpyxl import Workbook
wenjian=[["标题","链接"]]
#爬取功能
def gettitle():
response=request.urlopen(url)
html=response.read().decode("utf-8")
re_zz=re.compile(r'.*?href="(.*?)">(.*?)
list=re_zz.findall(html)
for address,title in list:
wenjian.append([title,address])
#存储功能
def cunchu():
wb=Workbook()
sheet=wb.active
sheet.title="菜鸟教程"
for i in range(0,len(wenjian)):
for j in range(0,2):
sheet.cell(row=i+1, column=j+1).value = wenjian[i][j]
wb.save("菜鸟教程.xlsx")
#入口函数
if __name__ == "__main__":
for i in range(1,26):
print("正在爬去第%s页....."%i)
url="https://c.runoob.com/examples/page/%s" %i
gettitle()
print("第%s页爬取完毕!"%i)
print("开始存储")
cunchu()
print("写入完毕!")
领取专属 10元无门槛券
私享最新 技术干货