我是靠谱客的博主 爱听歌故事,最近开发中收集的这篇文章主要介绍python爬虫菜鸟教程-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爬虫菜鸟教程-Python爬虫学习100练001的全部内容,希望文章能够帮你解决python爬虫菜鸟教程-Python爬虫学习100练001所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(90)

评论列表共有 0 条评论

立即
投稿
返回
顶部