首页 > 编程知识 正文

python爬虫 菜鸟教程Python爬虫学习100练001

时间:2023-05-03 11:35:59 阅读:284800 作者:3629

爬取菜鸟教程最新文章标题以及查看链接并写入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"

.?rel="external nofollow" 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("写入完毕!")

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。