Scrapy爬虫实例教程(二)数据存入MySQL
这里我们只是想获得文章的连接,types。
view_counts,这里我们需要编写 c. 具体代码如下(在zreadingCrawl中添加如下函数): 1 def parse(self, 1. 建立tutorial工程 1 scrapy startproject tutorial 上述命令运行完毕后会得到tutorial(或者自定义名称)的目录,item[content])) 6. 在settings.py中设置pipeline 当使用pipeline保存抓取内容时,所有的配置的工作已经结束,8 `content` text9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 4. 在items.py中编写需要抓取的内容 items.py是爬虫根据用户兴趣定义爬去内容的文件,读者可以自己实验试下,抓取需要内容 经过以上6步, item,对于文章内容链接我们可以直接请求响应的URL, 1 from twisted.enterprise import adbapi 2 import MySQLdb 3 import MySQLdb.cursors 4 from scrapy.crawler import Settings as settings 5 class TutorialPipeline(object): 6 7def __init__(self): 8 9dbargs = dict(10host = your host ,response): 2print In parsse paper content function...... 3# get the page number 5412.html 4# page_id = response.url.split(/)[-1].split(.)[0] ----- OK 5r =re.match(rd+。
这里也使用tutorial作为scrapy project名称(工程的名字可以有读者自己定制),%s,然后解析内容即可;而对于目录链接则可以从头解析(也即请求目录页然后进一步解析),我们的重点就是如何从网页中解析出我们所需要的内容。
这里我们要抓取文章标题,3 } 7. 解析网页,如在提取标题时,item[types],等运行完毕后查看数据库中的zreading表的内容,接下来,在spiders目录下,都需要进行处理,然后定义zreadingCrawl爬虫(继承scrapy的BaseSpider即可) 1 class zreadingCrawl(BaseSpider):2name = zreading # the name of spider3allowed_domain = [zreading.cn] # allowed domain for spiders4start_urls = [5 #the start url / the entrance of spider6] 具体的解析过程如下: a. 首先解析左岸的文章列表,6 `tags` varchar(50) DEFAULT NULL。
本人很喜欢看左岸的文章,author,对于每次的解析内容,在settings.py中加入一下代码 1 ITEM_PIPELINES = {2tutorial.pipelines.TutorialPipeline: 300,然后复制为xpath路径,绝无其他任何恶意,**dbargs)19 20 2122The default pipeline invoke function2324def process_item(self,在文章目录页中,建立如下数据表 1 CREATE TABLE `zreading` (2 `title` varchar(100) NOT NULL,item[view_count],并且系统中已经安装了Mysql数据库(有权限操作数据库),另一种则是文章列表的下一页,得到的内容前后包括很多空格,使用chrome的开发者工具,response): 2 3if response.url.endswith(html):4 5item = self.parsePaperContent(response) 6 7else: 8# get all the page links in list Page 9sel = Selector(response)10links = sel.xpath(//*[@id=content]/article/header/h4/a/@href).extract()11for link in links:12yield Request(link,item[author],阅读量及文章内容,spider):25res = self.dbpool.runInteraction(self.insert_into_table,阅读量等信息)在列表底端给出了下一个列表的链接,response.url.split(/)[-1]) 6page_id = r.group() 7# instantie the item 8zding = TutorialItem() 9sel = Selector(response)10#add tilte11title = sel.xpath(//div[@id=content]/article/header/h4/text()).extract()[0]12s_title = title.encode(utf-8)13zding[title] = s_title.lstrip().rstrip()14 15#add pub_date16pub_date = sel.xpath(//*[@id=+page_id+]/div[2]/span[1]/text()).extract()[0]17s_pub_date = pub_date.encode(utf8)18zding[pub_date] = s_pub_date.lstrip().rstrip()19 20#add author21author = sel.xpath(//*[@id=+page_id+]/div[2]/span[2]/a/text()).extract()[0]22s_author = author.encode(utf8)23zding[author] = s_author.lstrip().rstrip()24 25#add tags including type and paper tags26 27tags = sel.xpath(//*[@id=+page_id+]/div[2]/a/text()).extract()28tags = [s.encode(utf8) for s in tags]29zding[types] = tags[0]30zding[tags] = +.join(tags[1:])31 32#add view count33views = sel.xpath(//*[@id=+page_id+]/div[2]/span[3]/text()).extract()[0]34r = re.search(rd+。
定义相应的class。
17)18self.dbpool = adbapi.ConnectionPool(MySQLdb, # replace with you password14charset = utf8,item):29conn.execute(insert into zreading(title,有两种我们需要的链接。
例如可以存储到mysql或是json文件中,所有我们只需要提取文章题目链接的 href属性值即可。
11db = crawed,本例中使用chrome的开发工具,文中所有操作都是建立在scrapy已经配置完毕,item[pub_date], 本文将详细描述使用scrapy爬去左岸读书所有文章并存入本地MySql数据库中,如下图所示 2. 解析左岸文章结构 左岸读书为读者提供了一些优美文章,16use_unicode = True,就以此为例,callback=self.parse)13 14# get the next page to visitr15next_pages = sel.xpath(//*[@id=content]/div/a[@class=next]/@href).extract()16if len(next_pages) != 0:17yield Request(next_pages[0], *****声明:本帖纯粹是个人兴趣爱好,***** ,发布时间。
不再单独贴图。
在文章标题处右击,5 `types` varchar(50) DEFAULT NULL,文章发表日期,这是一个不断循环的过程。
在这一步我们需要编写网页解析的具体逻辑-如何处理网页,pub_date,3 `author` varchar(50) NOT NULL, #replace with you user name13passwd = user_password,conn。
views)35view_count = int(r.group())36zding[view_count] = view_count37#add content 38content = sel.xpath(//*[@id=+page_id+]/div[3]/p/text()).extract()39zding[content] = .join(content)40 41#return the item 42return zding 8. 在命令行下运行 1 scrapy crawl zreading 在屏幕中会闪解析过的网页和解析得到的item,这里不再赘述,本帖只是技术解析,12user = user_name,具体如下图所示 点击相应的文章题目可以链接到具体的文章内容页面。
callback=self.parse)18# record the list page19 20yield item 1 def parsePaperContent(self,恰逢学习scrapy,而且为了避免在数据库出现乱码,tags,7 `view_counts` varchar(20) DEFAULT 0,直至没有下一页为止,这里因为文章较长,%s,喜欢的读者可以自行订阅(在这里提博主打广告啦[不用谢^_^]) 站中所有文章都以列表的形式列出,item[tags],本例中选择存储到mysql中,在解析网页是就可以根据这个路径定位到你所需的内容,文章标签,点击检查,每篇文章链接都给出了文章摘要和相应的信息(如作者,在解析过程中需要借助一些开发插件,分类信息,%s,作者,%s。
一种是文章内容的链接。
以便让系统知道根据什么方式进行存储,使用tree命令可以查看tutorial的目录结构。
在此声明,读者可以根据自己实际需求选择相应的方式,绝无转载, 由上述可知, 为了避免读者混淆。
15cursorclass = MySQLdb.cursors.DictCursor,得到我们所需的内容,4 `pub_date` varchar(30) DEFAULT NULL,比如firefox的firebug,item)26return item27 28def insert_into_table(self。
用户可以根据自己的需求,新建zreading.py文件,chrome的开发者工具,需要设置相应的pipeline类。
文章类别,爬虫在解析网页时根据解析规则生成item类对象 这里根据我们步骤3中的数据类别建立如下类: 1 class TutorialItem(scrapy.Item): 2# define the fields for your item here like: 3# name = scrapy.Field() 4title = scrapy.Field() 5author = scrapy.Field() 6pub_date = scrapy.Field() 7types = scrapy.Field() 8tags = scrapy.Field() 9view_count = scrapy.Field()10content = scrapy.Field() 5. 编辑pipelines.py文件 pipelines.py是设置抓取内容存储方式的文件, 3. 建立mysql数据库 建立mysql数据库 crawed 1 create database crawed;2 use crawed; 在数据库中建立zreading数据表,%s),%s,所有数据都编码成utf8。
content) values(%s。
b. 在解析的过程中, (item[title],。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://www.juheyunku.com/sql/mysql/11033.shtml
相关文章
热门TAG
命令 外链 企业网站 白帽 php 织梦教程 dedecms修改内容 javascript 织梦 功能 标签 调用 详解 技巧 权重 服务器 网站流量 Dedecms 织梦cms HTML tags标签 python jquery教程 jquery windows 蜘蛛 搜索引擎 网站收录 JSP 实例解析最新文章
-
1 Mysql数据库的安装与配置
时间:2021-01-05
-
MySQL死锁套路之唯一索引下
时间:2020-12-28
-
MySQL找出未提交事务信息的
时间:2020-12-28
-
MySQL大小写敏感导致的问题
时间:2020-12-28
-
MySql8.0以上版本正确修改
时间:2020-12-26
-
mysql 5.7.17 以及workbench安装
时间:2020-12-26
-
mysql 5.7.17 安装配置方法图
时间:2020-12-26
-
解决MySQL8.0安装第一次登陆
时间:2020-12-26
热门文章
-
MySQL查询优化:LIMIT 1避免全表扫描提高查询
时间:2020-12-07
-
mysql安装图解 mysql图文安装教程(详细说明
时间:2020-12-23
-
MySQL8新特性:降序索引详解
时间:2020-12-23
-
MySQL死锁套路之唯一索引下批量插入顺序
时间:2020-12-28
-
MySQL DeadLock故障排查全过程记录
时间:2020-12-09
-
备忘单:提升你的 MariaDB 和 MySQL 数据库技
时间:2020-12-23
-
MySQL笔记之函数查询的使用
时间:2020-12-08
-
解决MySQL8.0安装第一次登陆修改密码时出
时间:2020-12-26
-
1 Mysql数据库的安装与配置
时间:2021-01-05
-
Mysql中文乱码以及导出为sql语句和Excel问题
时间:2020-12-07
