Python爬虫学习100练001

爬取菜鸟教程最新文章标题以及查看链接并写入excel文件中

-- 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("写入完毕!")

你可能感兴趣的:(Python爬虫学习100练001)