美文网首页
Python爬虫学习100练001

Python爬虫学习100练001

作者: 夜雨_87aa | 来源:发表于2018-03-24 23:14 被阅读0次

爬取菜鸟教程最新文章标题以及查看链接并写入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'<h4 class="panel-title">.?href="(.?)"><i.?</i>(.?)<span',re.S)
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

    爬取菜鸟教程最新文章标题以及查看链接并写入excel文件中 -- coding:utf-8 -- 2018年3月2...

  • Python爬虫学习100练003

    -- coding:utf-8 -- 2018年3月28日 爬取淘宝商品信息存到EXcel from urllib...

  • Python爬虫学习100练002

    爬取80s网站2018年电视剧作品主图片 -- coding:utf-8 -- 2018年3月25日 下载图片r...

  • Python爬虫学习(十六)初窥Scrapy

    Python爬虫学习(一)概述Python爬虫学习(二)urllib基础使用Python爬虫学习(三)urllib...

  • 资料

    Python爬虫系列(一)初期学习爬虫的拾遗与总结(11.4更) Python爬虫学习系列教程 Python爬虫学习手册

  • Python爬虫学习系列教程

    转自: 静觅»Python爬虫学习系列教程 Python爬虫学习系列教程 Python版本:2.7 一、爬虫入门 ...

  • 爬虫入门

    为什么要学习爬虫? Python做爬虫优势 关于Python网络爬虫,我们需要学习的有: 什么是爬虫? 网络爬虫(...

  • Python爬虫学习之小结(一)

    到目前为止,Python爬虫学习已经写了八篇文章,分别是: Python爬虫学习(一)概述Python爬虫学习(二...

  • python爬虫学习-day7-实战

    目录 python爬虫学习-day1 python爬虫学习-day2正则表达式 python爬虫学习-day3-B...

  • Python 基础爬虫目录

    目录 python爬虫学习-day1 python爬虫学习-day2正则表达式 python爬虫学习-day3-B...

网友评论

      本文标题:Python爬虫学习100练001

      本文链接:https://www.haomeiwen.com/subject/nqiccftx.html