python 第二周(第十天) 我的python成长记 一个月搞定python数据挖掘!(18) -mongodb

1. 首先导入工具from scrapy.selector import Selector

2. selectors的使用实例:response.selector.xpath(‘//span/text()‘).extract()

(1)选择title标签中text的文本内容        response.selector.xpath(‘//title/text()‘)        提供两个更简单的方法            response.xpath(‘//title/text()‘)            response.css(‘title::text‘)        例子:            response.css(‘img‘).xpath(‘@src‘).extract()            response.xpath(‘//div[@id="images"]/a/text()‘).extract_first()            response.xpath(‘//div[@id="not-exists"]/text()‘).extract_first(default=‘not-found‘)    (2)使用正则匹配的        response.xpath(‘//a[contains(@href, "image")]/text()‘).re(r‘Name:\s*(.*)‘)        response.xpath(‘//a[contains(@href, "image")]/text()‘).re_first(r‘Name:\s*(.*)‘)    (3)Working with relative XPaths        divs = response.xpath(‘//div‘)        for p in divs.xpath(‘.//p‘):             print p.extract()        for p in divs.xpath(‘p‘):             print p.extract()    (4)    (5)

官方实例:>>> links = response.xpath(‘//a[contains(@href, "image")]‘)>>> links.extract()[u‘<a href="image1.html">Name: My image 1 <br><img src="image1_thumb.jpg"></a>‘, u‘<a href="image2.html">Name: My image 2 <br><img src="image2_thumb.jpg"></a>‘, u‘<a href="image3.html">Name: My image 3 <br><img src="image3_thumb.jpg"></a>‘, u‘<a href="image4.html">Name: My image 4 <br><img src="image4_thumb.jpg"></a>‘, u‘<a href="image5.html">Name: My image 5 <br><img src="image5_thumb.jpg"></a>‘]

>>> for index, link in enumerate(links):...     args = (index, link.xpath(‘@href‘).extract(), link.xpath(‘img/@src‘).extract())...     print ‘Link number %d points to url %s and image %s‘ % args

Link number 0 points to url [u‘image1.html‘] and image [u‘image1_thumb.jpg‘]Link number 1 points to url [u‘image2.html‘] and image [u‘image2_thumb.jpg‘]Link number 2 points to url [u‘image3.html‘] and image [u‘image3_thumb.jpg‘]Link number 3 points to url [u‘image4.html‘] and image [u‘image4_thumb.jpg‘]Link number 4 points to url [u‘image5.html‘] and image [u‘image5_thumb.jpg‘]
时间: 2024-08-06 11:52:39

python 第二周(第十天) 我的python成长记 一个月搞定python数据挖掘!(18) -mongodb的相关文章

python 第一周(第二天) 我的python成长记 一个月搞定python数据挖掘!(03)

生成器表达式: (expr for iter in iterable [if condition]) 不对表达式求值,同列表解析 的区别 元祖: 同列表的区别,元组的内容是不能修改的,但是 ( 1,2,3,4,[3,4,5,6],True,(1,2,3)) 中的列表[3,4,5,6]中的元素3,4,5,6是可以更改的,存的是各个对象的指针 字符串: str01 + str02   生成新的字符串 str01 * 3  生成新的字符串 x = ['hello','world','python']

python 第二周(第十一天) 我的python成长记 一个月搞定python数据挖掘!(19) -scrapy + mongo

mongoDB 3.2之后默认是使用wireTiger引擎 在启动时更改存储引擎: mongod --storageEngine mmapv1 --dbpath d:\data\db 这样就可以解决mongvue不能查看文档的问题啦! 项目流程(步骤): 前去准备(安装scrapy pymongo mongodb ) 1. 生成项目目录: scrapy startproject  stack 2.itmes from scrapy import Item,Field class StackIte

python 第二周(第八天) 我的python成长记 一个月搞定python数据挖掘!(14)

from lxml import etree doubanhtml = ''''''doc = etree.fromstring(doubanhtml) for eachbook in doc.xpath('//dl/dd'): bookname = eachbook.xpath('a/text()')[0] bookurl = eachbook.xpath('a/@href')[0] pub = eachbook.xpath('div[@class="desc"]/text()')[

python 第一周(第一天) 我的python成长记 一个月搞定python数据挖掘!(01)

python的语法要点 1.注释 #表示注释,没有块注释 2. 变量(弱类型的语言),无须声明类型 例如   a,b = 'python' , 10 3. 内置数据类型 数字类型(整形,浮点型,复数)  布尔类型  字符串  None 4. 数学运算 除法取余: %  除法取整: //  乘方 **   不支持 ++ , -- 5. 比较操作符( > < 等等),逻辑操作符(and or not)  不支持 || , && 6. 条件和循环 代码缩进 if, if ... el

python 第一周(第三天) 我的python成长记 一个月搞定python数据挖掘!(04)

字符串 str 和 unicode str 字节流 unicode 字符流  (中文,英文,等等)  => 如何转换成计算机中的01代码呢? 出现了编码 ascii,  iso88591, utf8(unicode压缩之后的), unicode(用四个字节编码一个字符), utf32, gb2312 等等 那么gbk如何转换成utf8呢? gbk -> unicode ->  utf8 解码,编码 unicode -> utf8 编码 gbk-> unicode 解码 解决编

[译] 12步轻松搞定python装饰器 - 简书

body { font-family: Microsoft YaHei UI,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-serif,宋体, PMingLiU,serif; font-size: 10.5pt; line-height: 1.5; } html, body { } h1 { font-size:1.5em; font-weight:bold; } h2 { font-size:1.4em; font-weight:bo

12步轻松搞定python装饰器

12步轻松搞定python装饰器 呵呵!作为一名教python的老师,我发现学生们基本上一开始很难搞定python的装饰器,也许因为装饰器确实很难懂.搞定装饰器需要你了解一些函数式编程的概念,当然还有理解在python中定义和调用函数相关语法的一些特点. 我没法让装饰器变得简单,但是通过一步步的剖析,我也许能够让你在理解装饰器的时候更自信一点.因为装饰器很复杂,这篇文章将会很长(自己都说很长,还敢这么多废话blablabla...前戏就不继续翻译直接省略了) 1. 函数 在python中,函数通

深入浅出 Python 装饰器:16 步轻松搞定 Python 装饰器

Python的装饰器的英文名叫Decorator,当你看到这个英文名的时候,你可能会把其跟Design Pattern里的Decorator搞混了,其实这是完全不同的两个东西.虽然好像,他们要干的事都很相似--都是想要对一个已有的模块做一些"修饰工作",所谓修饰工作就是想给现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能)侵入到原有的模块中的代码里去.但是OO的Decorator简直就是一场恶梦,不信你就去看看wikipedia上的词条

编程学习第一步,让你20天搞定Python编程

大家好,欢迎大家阅读篇文章,Python是当前火爆的编程语言之一:从后台开发到自动化,从数据分析到人工智能,都有Python的身影.我们掌握Python,就有了进入这些行业的可能.下面简单介绍下老猫和这个专栏. Who Am I? 自称老猫,三线互联网公司开发人员一枚,30多岁依然奋斗开发一线,从事数据分析相关工作,每天在公司兢兢业业勤勤恳恳. 老猫,为什么三十多了还做一线开发? 最烦这么问的人,因为I Love It,编码的日子让我活的真实. 老猫,能不能说人话??? 因为上有老下有小,贷款压