软件测试爬虫,爬虫简单流程

#!/usr/bin/env python

#-*-coding=utf8-*-

from bs4 import BeautifulSoup

import re

html_doc = “””

The Dormouse’s story

The Dormouse’s story

Once upon a time there were three little sisters; and their names were

在计算机科学中 Elsie,

Lacie and

Tillie;

and they lived at the bottom of a well.

“””

#创建解析html的soup对象

soup = BeautifulSoup( html_doc,                #HTML文档

‘html.parser’,   #HTML解释器

from_encoding=’utf-8′ #HTML文档使用的编码

)

# 搜索节点标签方法 find_all/find(name,attrs,string)

#node = soup.find_all(‘a’,class_=’abc’,string=’python’) #class加下划线是为了区别关键字

#node.name   #获取你标签名字

#name[‘href’]#以字典的形式访问href属性的值

#node.get_text()#获取节点中的链接文字

print ‘获取所有链接’

node = soup.find_all(‘a’)

print ‘node’,node

for link in node:

print link.name, link[‘href’], link.get_text()

print ‘获取指定url链接’

node_link = soup.find(‘a’, class_=’sister’)

print node_link.name, node_link[‘href’], node_link.get_text()

print ‘使用正则表达进行模糊匹配’

node = soup.find(‘div’, class_=’para’).find_all(‘a’, href=re.compile(‘exa’))

for node_link in node:

print node_link.name, node_link[‘href’], node_link.get_text()

#1、compile()

#编译正则表达式模式,返回一个对象的模式。(可以把那些常用的正则表达式编译成正则表达式对象,这样可以提高一点效率。)

#格式:

#re.compile(pattern,flags=0)

#pattern: 编译时用的表达式字符串。

#flags 编译标志位,用于修改正则表达式的匹配方式,如:是否区分大小写,多行匹配等。常用的

相关资源:开源的爬虫软件Heritrix3.1.0_开源爬虫-Java工具类资源-CSDN文库

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年6月28日
下一篇 2021年6月28日

相关推荐