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

该博客介绍了如何使用Python的BeautifulSoup库解析HTML文档,特别是查找和提取页面上的链接。通过示例代码展示了find_all、find方法以及正则表达式在模糊匹配URL中的应用,为初学者提供了基础的网页抓取技巧。
摘要由CSDN通过智能技术生成

#!/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 编译标志位,用于修改正则表达式的匹配方式,如:是否区分大小写,多行匹配等。常用的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值