第一次用python 写的简单爬虫 记录在自己的博客
#python.py
from bs4 import BeautifulSoup
import urllib.request
from MySqlite import MySqlite global g_intid
g_intid=0
def GetBlogTileAndName(url):
res = urllib.request.urlopen(url)
html = res.read()
res.close()
str(html, 'utf-8')
soup=BeautifulSoup(html)
divs=soup.find_all(attrs={"class":"postTitle"}) for divname in divs:
print("title:=",divname.a.string,"href:=",divname.a["href"])
global g_intid
g_intid+=1
x=MySqlite()
x.InsertDate(g_intid,divname.a["href"],divname.a.string)
def GetBlogPage(url):
res = urllib.request.urlopen(url)
html = res.read()
res.close()
str(html, 'utf-8')
soup=BeautifulSoup(html)
divPager=soup.find(attrs={"class":"pager"})
print(divPager.string)
for i in range(1,8) :
url=r"http://www.cnblogs.com/FCoding/default.html?page="+str(i)
GetBlogTileAndName(url)
#MySqlite.py class MySqlite(object):
"""description of class"""
def __init__(self, *args):
return super().__init__(*args)
def callstr(self,str):
print(str) def InsertDate(self,id,url,title):
conn = sqlite3.connect(r"d:\123.db")
c=conn.cursor()
#try:
# c.execute('create table blog (ID intergeer,url text,title text , PRIMARY KEY(ID))')
#except ValueError:
# print("error My")
strExe="insert into blog values ({0}, \"{1}\",\"{2}\")".format(id,url,title)
print(id)
#c.execute('insert into blog values (last_insert_rowid(),url,title)')
c.execute(strExe)
conn.commit()
c.close()
conn.close() def GetDate(self):
import sqlite3
conn = sqlite3.connect(r"d:\123.db")
c=conn.cursor()
res=c.execute("select count(*) from blog")
res=c.fetchone()
print(res[0])
data=c.execute("select * from blog")
for item in data:
for ite in item:
print(ite)
conn.commit()
c.close()
conn.close()
简述一下功能:
通过urllib 下载网页 使用BeautifulSoup 解析
调用find_all(attrs={"class":"postTitle"})
找到HTML 中所有class=posttitle 的tag
然后遍历 取出title 和href 保存到数据库中
此程序 无容错。新手无笑!
第一次用python 写的简单爬虫 记录在自己的博客的更多相关文章
- 用node.js写一个简单爬虫,并将数据导出为 excel 文件
引子 最近折腾node,最开始像无头苍蝇一样到处找资料,然而多数没什么卵用,都在瞎比比.在一阵瞎搞后,我来分享一下初步学习node的三个过程: 1 撸一遍NODE入门,对其有个基本的了解: 2 撸一遍 ...
- Python爬虫,看看我最近博客都写了啥,带你制作高逼格的数据聚合云图
转载请标明出处: http://blog.csdn.net/forezp/article/details/70198541 本文出自方志朋的博客 今天一时兴起,想用python爬爬自己的博客,通过数据 ...
- 用Python写一个简单的Web框架
一.概述 二.从demo_app开始 三.WSGI中的application 四.区分URL 五.重构 1.正则匹配URL 2.DRY 3.抽象出框架 六.参考 一.概述 在Python中,WSGI( ...
- [Python爬虫笔记][随意找个博客入门(一)]
[Python爬虫笔记][随意找个博客入门(一)] 标签(空格分隔): Python 爬虫 2016年暑假 来源博客:挣脱不足与蒙昧 1.简单的爬取特定url的html代码 import urllib ...
- 利用爬虫将Yuan先生的博客文章爬取下来
由于一次巧遇,我阅读了Yuan先生的一篇博客文章,感觉从Yuan先生得博客学到很多东西,很喜欢他得文章.于是我就关注了他,并且想阅读更多出自他手笔得博客文章,无奈,可能Yuan先生不想公开自己得博客吧 ...
- Python简单爬虫记录
为了避免自己忘了Python的爬虫相关知识和流程,下面简单的记录一下爬虫的基本要求和编程问题!! 简单了解了一下,爬虫的方法很多,我简单的使用了已经做好的库requests来获取网页信息和Beauti ...
- 用python写个简单的小程序,编译成exe跑在win10上
每天的工作其实很无聊,早知道应该去IT公司闯荡的.最近的工作内容是每逢一个整点,从早7点到晚11点,去查一次客流数据,整理到表格中,上交给素未蒙面的上线,由他呈交领导查阅. 人的精力毕竟是有限的,所以 ...
- 【Python项目】简单爬虫批量获取资源网站的下载链接
简单爬虫批量获取资源网站的下载链接 项目链接:https://github.com/RealIvyWong/GotDownloadURL 1 由来 自己在收集剧集资源的时候,这些网站的下载链接还要手动 ...
- 用Python写的简单脚本更新本地hosts
这两天Google墙得严重,于是就产生了做个一键更新hosts的脚本的想法. 由于正在学习Python,理所当然用Python来写这个脚本了. 接触比较多的就是urllib2这个库,习惯性的impor ...
随机推荐
- 582. Kill Process杀死所有子代
[抄题]: Given n processes, each process has a unique PID (process id) and its PPID (parent process id) ...
- Java14-java语法基础(十三)接口
Java14-java语法基础(十三)接口 一.接口 1.接口的作用 Java出于安全性.简化程序结构的考虑,不支持多继承而仅支持单继承.然而实际问题中很多情况下仅仅依靠单继承并不能将复杂的问题描述清 ...
- ELK+SpringBoot+Logback离线安装及配置
ELK+SpringBoot+Logback 离线安装及配置 版本 v1.0 编写时间 2018/6/11 编写人 xxx 目录 一. ELK介绍2 二. 安装环境2 三. Elasticse ...
- iOS10 UIImageWriteToSavedPhotosAlbum程序奔溃crash解决办法
Xcode报错: This app has crashed because it attempted to access privacy-sensitive data without a usage ...
- springmvc mybatis shiro ios android构建cms系统
开发语言: java.ios.android 部署平台: linux.window jdk版本:JDK1.7以上版本 开发工具: eclipse.idea等 服务器中间件:Tomcat 6.7.Jbo ...
- SPARK安装二:HADOOP集群部署
一.hadoop下载 使用2.7.6版本,因为公司生产环境是这个版本 cd /opt wget http://mirrors.hust.edu.cn/apache/hadoop/common/hado ...
- AX_NumberSeq
NumberSeq::newGetNum(NumberSeqReference::findReference(typeId2ExtendedTypeId(typeid(InventTransId))) ...
- Axure RP Extension for Chrome 插件安装
描述 我的chmod浏览器上不去谷歌商店,我用的是蓝灯,登上商店后搜索Axure RP Extension for Chrome,下载安装,完成后进入这个插件的详细信息: 使用 打开用axure生成的 ...
- ABP框架系列之八:(Introduction-介绍)
Introduction We are creating different applications based on different needs. But implementing commo ...
- noip第19课资料