前言

python初级爬虫只需要掌握以下四个技术

  • find 字符串函数
  • 列表切片list[-x:-y]
  • 文件读写操作
  • 循环体while

原理:
网页上的任何东西都对应着源代码, 所以爬虫的原理就是对网页上的源代码的爬取和访问两部分。
第一步:1 先对待爬取东西的代码截取,对于单篇文章而言

 <a title="" target="_blank" href="http://blog.sina.com.cn/s/blog_4701280b0102wrup.html">写给那个茶水妹的《乘风破浪》诞生…</a>

这是文章对应的代码部分,我们需要切取出所需的url为
http://blog.sina.com.cn/s/blog_4701280b0102wrup.html
第二步:访问该url并存盘
导入urllib内建库
content = urllib.urlopen(url).read()
filename=”xxx”
输出HTML
with open(filename,”w”) as f:
f.write(content)
或者输出TXT
with open(“…../”+filename+”.txt”,”w”) as f:
f.write(content)
但是对于首页的所有文章,则是读取首页的所有内容urllib.urlopen(url).read(),并在所读取的内容中截取文章的url并存盘。

数据源:韩寒博客
http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html

第一部分 :下载单篇博文存储本地

第一步:分析html源文件
邮件审查元素可以看到网页的源文件(chrome快捷键F12),然后在源文件中查找文章名字:写给那个茶水妹的《乘风破浪》诞生…,在body中使用快捷键ctrl+F 查找:

找到其文章名字段落规则为:

 <a title="" target="_blank" href="http://blog.sina.com.cn/s/blog_4701280b0102wrup.html">写给那个茶水妹的《乘风破浪》诞生…</a>

则在此字符串查询所需要的部分。

第二步:代码处理以及url的提取
导入urllib内建库
content = urllib.urlopen(url).read()
filename=”xxx”
输出HTML
with open(filename,”w”) as f:
f.write(content)
或者输出TXT
with open(“…../”+filename+”.txt”,”w”) as f:
f.write(content)

代码实现

import urllib# 使用转义符
str0 ="<a title=\"\" target=\"_blank\" href=\"http://blog.sina.com.cn/s/blog_4701280b0102wrup.html\">写给那个茶水妹的《乘风破浪》诞生…</a>"
# 其规则是 href="连接"   ">题目</a>"# 截取题目索引
title_1 = str0.find(r">")
title_2 = str0.find(r"</a>")
title = str0[title_1+1:title_2]
print title
# 截取http连接索引
href = str0.find(r"href=")
html = str0.find(r".html")
# 截取 url
url = str0[href+6:html+5]# 读出的是html码,type是str
content = urllib.urlopen(url).read()m = url.find("blog_")
filename = url[m:]
filename_1 = "F://python/PyCharmWorkpalce/Crawler/pacong_data/" + filename# 写成html
with open(filename_1 ,"w+") as f:f.write(content)
# 写成txt文件
with open(filename_1 + ".txt","w+") as f:f.write(content)
# 存成题目的txt文件
# 因为编译器是utf-8编码,则需要unicode编译
with open(unicode(filename_0 + title + ".txt","utf-8"),"w+") as f:f.write(content)

输出结果

第二部分:爬取首页的全部文章存入本地

同爬取单篇文章相类似,爬取首页的所有文章则需要读取首页的全部源代码,并对文章url进行截取,分析文章url

<a title="" target="_blank" href="http://blog.sina.com.cn/s/blog_4701280b0102wrup.html">写给那个茶水妹的《乘风破浪》诞生…</a></span> <a title="" target="_blank" href="http://blog.sina.com.cn/s/blog_4701280b0102eo83.html">《论电影的七个元素》——关于我对电…</a></span>                                               

则需要在整体所读内容中找到第一篇文章的规律。特殊字段为

# -*- coding: utf-8 -*-
# auther : santi
# function :
# time :import urllib
import time
# 直接读取首页的全部内容
str0 = "http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html"
con = urllib.urlopen(str0).read()# 将con打印出来观察其文章题目规则
# <a title="" target="_blank" href="http://blog.sina.com.cn/s/blog_4701280b0102elmo.html">2013年09月27日</a></span>
# <a title="" target="_blank" href="http://blog.sina.com.cn/s/blog_4701280b0102wruo.html">写给那个茶水妹的《乘风破浪》诞生…</a></span>with open(r"F:\python\PyCharmWorkpalce\Crawler\pacong_data\context.txt","w") as f:f.write(con)url_all = [""] * 60
url_name = [""] * 60
index = con.find("<a title=\"\" target=\"_blank")
href = con.find("href=\"http:",index)
html = con.find(".html\">",href)
title = con.find("</a></span>",html)i = 0
# find函数找不到会返回-1,则说明全部爬取,直接跳出while循环。
while index != -1 and href != -1 and html != -1 and title != -1 or  i < 50 :url_all[i] = con[href+6:html+5]url_name[i] = con[html+7:title]print "finding...   " + url_all[i]index = con.find("<a title=\"\" target=\"_blank",title)href = con.find("href=\"http:",index)html = con.find(".html\">",href)title = con.find("</a></span>",html)i += 1else:print "Find End!"# 本地存储
# http://blog.sina.com.cn/s/blog_4701280b0102wrup.htmlm_0 = url_all[0].find("blog_")
m_1 = url_all[0].find(".html")+5
filename_0 = "F://python/PyCharmWorkpalce/Crawler/pacong_data/"j = 0
while j < i:filename_1 = url_all[j][m_0:m_1]content = urllib.urlopen(url_all[j]).read()print "downloading.... " + filename_1with open(filename_0 + filename_1,"w+") as f:f.write(content)with open(filename_0 + filename_1 + ".txt","w+") as f:f.write(content)with open(unicode(filename_0 + url_name[j] + ".txt","utf-8"),"w+") as f:f.write(content)time.sleep(15)j += 1print "Download article finished! "

输出结果:

finding...   http://blog.sina.com.cn/s/blog_4701280b0102wrup.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102wruo.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102eohi.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102eo83.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102elmo.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102eksm.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102ek51.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102egl0.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102ef4t.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102edcd.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102ecxd.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102eck1.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102ec39.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102eb8d.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102eb6w.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102eau0.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e85j.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e7wj.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e7vx.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e7pk.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e7er.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e63p.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e5np.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e4qq.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e4gf.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e4c3.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e490.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e42a.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e3v6.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e3nr.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e150.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e11n.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e0th.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e0p3.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e0l4.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e0ib.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e0hj.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e0fm.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e0eu.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e0ak.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e07s.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e074.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e06b.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e061.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102e02q.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102dz9f.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102dz84.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102dz5s.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102dyao.html
finding...   http://blog.sina.com.cn/s/blog_4701280b0102dxmp.html
Find End!
downloading.... blog_4701280b0102wrup.html
downloading.... blog_4701280b0102wruo.html
downloading.... blog_4701280b0102eohi.html
downloading.... blog_4701280b0102eo83.html
downloading.... blog_4701280b0102elmo.html
downloading.... blog_4701280b0102eksm.html
downloading.... blog_4701280b0102ek51.html
downloading.... blog_4701280b0102egl0.html
downloading.... blog_4701280b0102ef4t.html
downloading.... blog_4701280b0102edcd.html
downloading.... blog_4701280b0102ecxd.html
downloading.... blog_4701280b0102eck1.html
downloading.... blog_4701280b0102ec39.html
downloading.... blog_4701280b0102eb8d.html
downloading.... blog_4701280b0102eb6w.html
downloading.... blog_4701280b0102eau0.html
downloading.... blog_4701280b0102e85j.html
downloading.... blog_4701280b0102e7wj.html
downloading.... blog_4701280b0102e7vx.html
downloading.... blog_4701280b0102e7pk.html
downloading.... blog_4701280b0102e7er.html
downloading.... blog_4701280b0102e63p.html
downloading.... blog_4701280b0102e5np.html
downloading.... blog_4701280b0102e4qq.html
downloading.... blog_4701280b0102e4gf.html
downloading.... blog_4701280b0102e4c3.html
downloading.... blog_4701280b0102e490.html
downloading.... blog_4701280b0102e42a.html
downloading.... blog_4701280b0102e3v6.html
downloading.... blog_4701280b0102e3nr.html
downloading.... blog_4701280b0102e150.html
downloading.... blog_4701280b0102e11n.html
downloading.... blog_4701280b0102e0th.html
downloading.... blog_4701280b0102e0p3.html
downloading.... blog_4701280b0102e0l4.html
downloading.... blog_4701280b0102e0ib.html
downloading.... blog_4701280b0102e0hj.html
downloading.... blog_4701280b0102e0fm.html
downloading.... blog_4701280b0102e0eu.html
downloading.... blog_4701280b0102e0ak.html
downloading.... blog_4701280b0102e07s.html
downloading.... blog_4701280b0102e074.html
downloading.... blog_4701280b0102e06b.html
downloading.... blog_4701280b0102e061.html
downloading.... blog_4701280b0102e02q.html
downloading.... blog_4701280b0102dz9f.html
downloading.... blog_4701280b0102dz84.html
downloading.... blog_4701280b0102dz5s.html
downloading.... blog_4701280b0102dyao.html
downloading.... blog_4701280b0102dxmp.html
Download article finished! 

下载示意图:

《python初级爬虫》(一)相关推荐

  1. 关于《那些年啊,那些事——一个程序员的奋斗史》——24提到的鼻炎治疗方法

    在<那些年啊,那些事--一个程序员的奋斗史>--24中有提到用清水治疗鼻炎的方法,很多朋友在留言或email都有问到这问题,在此一并做个回答.         简单说说norains的鼻炎 ...

  2. 《那些年啊,那些事——一个程序员的奋斗史》十四(完结)

    125 距离离职交接的一个月时间还剩几天,本来应该是平淡无事的,却没想到最后还是波澜四起.昨天下班前,公司突然停了电.这本是件普通得不能再普通的事情,可没想到过了一会来电了,或许是波峰电压太大,或许是 ...

  3. 《那些年啊,那些事——一个程序员的奋斗史》——128 (终章)

    周五,如约而至.张文香果然没有被邀请,武总自然也是被排除之列,来的都是平时大家谈得比较来的.几个月不见,小黑和小蓝还是老样子,但精神相比以前是好多了:麦吉则没什么太大的变化,倒是陈莉胖了不少,颇有一番 ...

  4. 关于 那些年啊,那些事——一个程序员的奋斗史 ——24提到的鼻炎治疗方法

    分享一下我老师大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow 在<那些年啊 ...

  5. 关于 那些年啊 那些事 一个程序员的奋斗史 24提到的鼻炎治疗方法

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 在< ...

  6. 《那些年啊,那些事——一个程序员的奋斗史》——79

    旧的4.3'主板是废弃了,新的带CMMB的板子开始了.对于新的板子来说,其实只是在旧的板子上做改动,比如删掉蓝牙模块啊,去掉视频输入啊等:最大的不同,只是根据Telechips的原理图增加CMMB部分 ...

  7. 那些年啊,那些事——一个程序员的奋斗史 ——23

    分享一下我老师大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow "开会咯 ...

  8. 《那些年啊,那些事——一个程序员的奋斗史》——23

    "开会咯!"黄华中在研发部嚷了一下,让大家到外面的会议桌去.说起来,自从段伏枥来到这公司以来,已经差不多有好几个月,却连一个会都没有开过,甚至是小一点的内部会议都没有.为什么突然间 ...

  9. 《那些年啊,那些事——一个程序员的奋斗史》——117

    这段时间段伏枥在折腾.NET Micro Framework的时候,终于体会到什么叫举步维艰了.因为这玩意没有操作系统做支撑,直接是操作硬件,有时候出问题了,根本就不知道是如何引起的. 比如说,在移植 ...

  10. 《那些年啊,那些事——一个程序员的奋斗史》——38

    自从会见了律师之后,时间又过了一周,期间还是和往常没什么两样,段伏枥依然还是赶最后一班公交,周六还是依然需要加班.不过,经过王健的努力,这板子终究还是跑起来了.不过,这跑起来的概念也就是能够进入Win ...

最新文章

  1. TensorFlow 2.0开发者预览版发布
  2. ['1','2','3'].map(parseInt)
  3. CentOS 7.X 安装 Gitlab 笔记
  4. MVC框架浅析(基于PHP)
  5. HarmonyOS之常用组件ScrollView的功能和使用
  6. php读取西门子plc_PLC对模拟量信号,是怎么进行处理的?
  7. Warensoft Unity3D通信库使用向导4-SQL SERVER访问组件使用说明
  8. ug打开服务器文件保存不了,UG突然说保存不了, 你都是怎么处理的呢?
  9. Python中整除、求余运算
  10. html导航栏的颜色怎么改变,bootstrap如何更改导航栏颜色
  11. Spring MVC中的Model, ModelMap, 和ModelAndView
  12. windows 命令行 通过驱动名删除打印机
  13. import nose ImportError: No module named 'nose'
  14. dnw下载文件时can not open /dev/secbulk0的解决方法
  15. 前罗辑思维撰稿人李源,首度在喜马拉雅FM解读大英帝国
  16. Hive SQL 计算指定日期本周的第一天和最后一天
  17. 【Vue】- created()方法
  18. ETCgame移动端上线,预测游戏世界杯预测触手可及
  19. 【客服狂收offer秘籍】面试常见问题拆解
  20. 记录 2015 年年初跳槽的经历!

热门文章

  1. 谷歌中国团队遭哄抢 部分员工私下接洽新公司
  2. android nio wifi 拥堵 超时,提高WiFi上网速度,改变道路拥挤的方法
  3. 干货,值得收藏!Python 操作 Excel 报表自动化指南!
  4. 如何利用PDF编辑软件将pdf文字修改
  5. 软件测试学习路线全面攻略,掌握这些技术轻松15K
  6. 使用 JavaScript 将 JSON 数据动态转换为 HTML 表
  7. OEM和ODM的区别
  8. 微信公众号文章采集的几种方案
  9. 光阴深处岁月静好可待流年
  10. 删库跑路大全 删库跑路