2018-06-24

网页解析方法

1、BeautifulSoup

from bs4 import BeatifulSopu

soup=BeautifulSopu(html,'lxml')

获取属性的方法:

soup.p.attrs    输出全部属性

soup.p.attrs['name']获取指定属实

soup.p['name']使用字典形式获取

获取文本的方法:

soup.p.string使用属性

soup.p.get_text()使用方法

获取直接子节点

soup.p.contents返回所有的子结点一个列表

soup.p.children返回子结点的一个生成器,使用使用迭待输出

获取子孙节点

soup.p.descendants返回子孙节点的一个生成器,包括子节点、子孙节点

返回父节点

soup.a.parent

soup.a.parents获取所有父辈

获取兄弟节点

soup.a.next_sibling

soup.a.previous_sibling

soup.a.next_siblings

soup.a.previous_siblings

元素查找

def find_all(self, name=None, attrs={}, recursive=True, text=None, limit=None, **kwargs):与findAll是同一函数

此处的name为标签名,如ul,li等也可以传递attrs参数

使用text参数匹配节点的文本,传入字符串或者传入正则表达式

soup.find_all(text=re.compile('link')))把包含link的文本结果输出,类型为列表

其他方法

find()

find_parents()

find_parent()

find_next_sibling()

find_next_siblings()

find_previous_siblings()

find_previous_sibling()

find_all_next()

find_next()

find_all_previous()

find_previous()

另外BeautifulSopu中也提供了CSS选择器

soup.select('ul  li'))

2、pyquery使用

from pyquery import PyQuery as pq

以下三种方式

doc=pq(html)

doc=pq(url)

doc=pq(文件名)

print(doc('#container  .list li'))

获取子节点:

items=doc('.list')

items.find('li')使用find可以返回所有子孙节点,如果只想返 回子节点,则使用children()

items.children()此时可以指定需要筛选出的子节点,增加参数即可,如

items.children('.activer')

获取父节点

items=doc('.list')

items.parent()及items.parents()

如果要获取指定的父节点可使用items.parents('.wrap')

获取兄弟节点

items.siblings()获取所有的氏兄弟节点

items.siblings('.active')获取指定的兄弟节点

元素的输出

1、针对单个元素,可以直接打印输入出,也可以转为字符串

li=doc('.item1')

print(li)或print(str(li))

2、如果有多个节点则需要遍历输出

doc('li').items()返回一个生成器,此处的items与上面代码的items是不同的,此处是固定值,上面讲的是随便取的一个名字

获取属性

a=doc('.item')

以下2种方式获取属性

print(a.attr('href'))

print(a.attr.href)

,如果属性是多个,则需要遍历 才能输出

as=doc('li').items()

for a in as:

    print(a.attr('href')

获取文件

a=doc('.item')

print(a.text())获取内部的文本信息

print(a.html())获取节点内的html文本

另外html()返回第一个节点的内部html文本,而text()返回的是所以节点的纯文本,返回中间使用空格分开的字符串

其他方法

addClass()  removeClass()  remove()

属性值修改

li=doc('.item')

li.attr('name','link')

li.text('changed')

li.html(' ahndagdaf')

如果attr()方法中传入一个参数的属性名,则此时是获取这个属性值,如果传入2个参数,则是修改属性值,如果text(),html()没有传入参数,则是获取 文本,如果传入了参数,则是进行赋值。

http://www.w3school.com.cn/css/index.asp

支持伪类选择器

doc=pq(html)

li=doc('li:first-child')

first-child  last-child   nth-child(2)  gt(2)   nth-child(2n)  contains(second)


3、lxml (xpath)

from lxml import etree

以下2种方式

html=etree.HTML(text)#已知字符串时使用

html=etree.parse('./test.html',etree.HTMLParser())#已知文件名时使用

获取属性值、文本值

此时就可以使用xpath进行匹配了@取属性  text()取文本

result=html.xpath('//li/a/@hre')

html.xpath('//li[@class='item']

html.xpath('//li[@class='item'/text()]

如果有多个属性值时,可以使用contains()函数

result=html.xpath('//li/a[contains(@class,“li”)]/p/test()')

多属性匹配可以使用and

result=html.xpath('//li[contains(@clall,'li') and @name='tang']/a/text()')

按序选择

li[1]

li[last()]

li[position()<3]

li[last()-2]

http://www.w3school.com.cn/xpath/xpath_functions.asp

4、selector解析

选择器的使用可以分为下面的三步:

在scrapy中有2中方法调用selector

一种是from scrapy.selector import Selector

另一种是from scrapy import Selector

导入选择器from scrapy.selector import Selector

创建选择器实例selector = Selector(response=response)

使用选择器selector.xpath()或者selector.css()

Scrapy selector是以 文字(text) 或 TextResponse 构造的 Selector 实例。 其根据输入的类型自动选择最优的分析方法(XML vs HTML):

>>> from scrapy.selector import Selector

>>> from scrapy.http import HtmlResponse

以文字构造:

>>> body='good'

>>> Selector(text=body).xpath('//span/text()').extract()[u'good']

以response构造:

>>> response=HtmlResponse(url='http://example.com',body=body)

>>> Selector(response=response).xpath('//span/text()').extract()[u'good']

其他方法

xpath选择器中还有一个.re()方法,可以返回unicode字符串的列表

你可能感兴趣的:(2018-06-24)