精通scrapy网络爬虫——第三章 response.xpath()and css()基础语法

首先创建一个用于演示的HTML文档,并构造一个HtmlResponse对象

from scrapy.selector import Selector
from scrapy.http import HtmlResponse

body = '''

    
        
        Example website
    
    
        
    

'''
response=HtmlResponse(url='http://example.com',body=body,encoding='utf-8')
  1. /:描述一个从跟开始的绝对路径
#/:描述一个从跟开始的绝对路径
print(response.xpath('/html'))
print(response.xpath('/html/head'))
#[]
#[]
  1. E1/E2:选中E1子节点的所有E2
#E1/E2:选中E1子节点的所有E2
print(response.xpath('/html/body/div/a'))
#[,
# ,
# ,
# ,
# ]
  1. //E: 选中文档中的所有E,无论在什么位置
#//E: 选中文档中的所有E,无论在什么位置
print(response.xpath('//a'))
#[,
# ,
# ,
# ,
# ]
  1. E1//E2:选中E1后代节点中的所有E2,无论在后代的什么位置,例如E1/E4/E5/E2 等价于E1//E2
#E1//E2:选中E1后代节点中的所有E2,无论在后代的什么位置,例如E1/E4/E5/E2 等价于E1//E2
print(response.xpath('/html/body//img'))
#[,
# ,
# ,
# ,
# ]
  1. E/text():选中E的文本子节点
#E/text():选中E的文本子节点
print(response.xpath('//a/text()'))
#[,
# ,
# ,
# ,
# ]
  1. E/*:选中E的所有子节点
#E/*:选中E的所有子节点
#选中html的所有元素子节点,html有两个元素子节点head和body
print(response.xpath('/html/*'))
#[,
# ]
  1. */E:选中孙节点的所有E
#*/E:选中孙节点的所有E
print(response.xpath('//div/*/img'))
#[,
# ,
# ,
# ,
# ]

  1. E/@ATTR:选中E的attr属性
#E/@ATTR:选中E的attr属性
print(response.xpath('//img/@src'))
#[,
# ,
# ,
# ,
# ]
  1. //@attr:选中文档中所有的attr属性
#//@attr:选中文档中所有的attr属性
print(response.xpath('//@href'))
#[,
# ,
# ,
# ,
# ,
# ]

  1. 10.//E/@*:选中E的所有属性
#//E/@*:选中E的所有属性
print(response.xpath('//a[1]/img/@*'))
#[
  1. #.:选中当前节点,用来描述相对路径
# #.:选中当前节点,用来描述相对路径
s=response.xpath('//a')[0]
print(s) #
# 如果我们想要获得当前节点的子节点a[0]/img,那么使用s.xpath('/img')是错误的,它会得到所有a的子节点
print(s.xpath('//img'))
#[,
# ,
# ,
# ,
# ]
print(s.xpath('./img'))  #加上 . 后会正确
#[]
  1. …:选中当前节点的父节点
# ..:选中当前节点的父节点
print(response.xpath('//img/..'))
#同print(response.xpath('//a'))
  1. node[谓语]:谓语通常用来查找某个特定的节点或者包含某个特定值的节点
#node[谓语]:谓语通常用来查找某个特定的节点或者包含某个特定值的节点
print(response.xpath('//a[3]')) #[]

#使用last()函数,选择最后一个
print(response.xpath('//a[last()]')) #[]

#使用position()函数,选择前三个
print(response.xpath('//a[position()<=3]'))
#[,
# ,
# ]

#选择含有id属性的div
print(response.xpath('//div[@id]'))
#[]
#选择含有id属性且值为"image"的div
print(response.xpath('//div[@id="image"]'))
#[]
  • 两个函数:string() contain()
from scrapy.selector import Selector
text='Click here to go to the Next Page'
s=Selector(text=text)
print(s.xpath('/html/body/a/strong/text()'))   #text只是截取了部分HTMl,但是在xpath寻找路径的过程中依旧要写出来,或者//a
#[]
print(s.xpath('string(/html/body/a/strong)').extract())   #['Next Page']
print(s.xpath('//a/text()').extract())  #['Click here to go to the ']
#string()方法可以得到两个不连续的字串变为连续
print(s.xpath('string(//a)').extract())  #['Click here to go to the Next Page']

text1= '''

Hello World!

Hello Scrapy!

'''
#contains(str1,str2):判断str1中是否包含str2,返回布尔值 s1=Selector(text=text1) print(s1.xpath('//p[contains(@class,"small")]'))

css()

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from scrapy.selector import Selector
from scrapy.http import HtmlResponse

body = '''

    
        
        Example website
    
    
        
         
    

'''
response=HtmlResponse(url='http://example.com',body=body,encoding='utf-8')

#E选中E元素
print(response.css('img'))
#[,
# ,
# ,
# ,
# ]

#E1,E2,选中E1和E2元素
print(response.css('base,title'))
#[,
# ]

##E1 E2 :选中E1后代中的E2元素
[print(response.css('div img'))]
#输出与第一中一样

#E1>E2:选中E1子元素中的E2元素
print(response.css('body>div'))
#[]

#[attr]:选中包含attr属性的元素
print(response.css('[href]'))
#结果自己看

#[attr=value] : 选中包含attr属性且值为value的元素
print(response.css('[id="image-2"]'))
#[]


# E:nth-child(n)选中E下的第n个子元素
print(response.css('div>a:nth-child(1)'))#div下的a的第一个子元素有两个
#[,
# ]
print(response.css('div[id="image-2"]>a:nth-child(1)'))#当选中div属性为image-2下的a的第一个子元素时,就只有一个
#[]
print(response.css('div:nth-child(2)>a:nth-child(2)'))
#[]


#first-child and last-child
print(response.css('div[id="image-2"]>a:first-child'))

print(response.css('div:first-child>a:last-child'))
#[]

你可能感兴趣的:(scrapy网络爬虫)