python 获取链接BeautifulSoup4 取链接

python中的BeautifulSoup使用小结
时间: 09:57:18
&&&& 阅读:1121
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&1.安装
pip install beautifulsoup4
2.代码文件中导入
from bs4 import BeautifulSoup
Python标准库
BeautifulSoup(markup, “html.parser”)
Python的内置标准库
执行速度适中
文档容错能力强
Python 2.7.3 or 3.2.2)前 的版本中文档容错能力差
lxml HTML 解析器
BeautifulSoup(markup, “lxml”)
文档容错能力强
需要安装C语言库
lxml XML 解析器
BeautifulSoup(markup, [“lxml”, “xml”])BeautifulSoup(markup, “xml”)
唯一支持XML的解析器
需要安装C语言库
BeautifulSoup(markup, “html5lib”)
最好的容错性
以浏览器的方式解析文档
生成HTML5格式的文档
不依赖外部扩展
r = requests.get(‘http://www.baidu.com/‘)
soup = BeautifulSoup(r.text, ‘html.parser‘)
soup = BeautifulSoup(open(‘index.html‘))
print soup.prettify()
#美化HTML代码显示
Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象:
#显示第一个同名标签
soup.head.name #显示标签名称,这里输出‘head’
soup.head.attrs
#显示标签的属性,以字典形式返回所有属性
soup.head[‘class‘] #显示head标签的class属性值
soup.head[‘class‘] = ‘newclass‘ #修改head标签class属性值为‘newclass’
del soup.head[‘class‘] #删除head标签的class属性
soup.head.string
#获取标签内的正文内容,返回值类型为NavigableString
soup.body.contents[0]
#获取body标签的第一个子结点,contents是一个列表
for child in soup.body.children:
print(child.string)
#children与contents一样,都获取全部直接子结点,只不过children是一个生成器,需遍历取出
for child in soup.body.descendants:
print(child.string)
#递归遍历获取自身下面所有层级的所有节点,从最高一层列出然后下一层,直到最底层。
for string in soup.body.children.strings:
print(repr(string))
#strings获取多个正文内容,需遍历取出,stripped_strings去掉每个字符串前后空格及空行,多余的空格或空行全部去掉,使用方法与strings一致
soup.body.parent #获取父节点
for parent in soup.head.title.string.parents:
print(parent.name)
#遍历上级节点路径,返回结果为title,head,html
.next_sibling
#下一兄弟节点
.previous_sibling
#上一兄弟节点
.next_siblings &#往下遍历所有兄弟节点
.previous_siblings
#往上遍历所有兄弟节点
.next_element &
#下一节点,不分层级
.previous_element
#上一节点,不分层级
.next_elements &
#往下顺序遍历所有节点,不分层级
.previous_elements
#往上遍历所有节点,不分层级
7.搜索查找标签
find_all( name , attrs , recursive , text , **kwargs )
#(1)name参数
soup.find_all(‘a‘)
#查找所有a标签
soup.body.div.find_all(‘a‘)
#查找body下面第一个div中的所有a标签
for tag in soup.find_all(re.compile(‘^b‘));
print(tag.name)
#正则表达式查找所有以b开头的标签
soup.find_all([‘a‘,‘b‘])
#列表查找,返回所有a标签和b标签
soup.find_all(True)
#为True时,所有标签都满足搜索条件,返回所有标签
#以下为自定义过滤条件,筛选满足自定义条件的标签
def has_class_but_no_id(tag):
&&&&return tag.has_attr(‘class‘) and not tag.has_attr(‘id‘)
soup.find_all(has_class_but_no_id)
#返回所有具有class属性但无id属性的标签
#(2)attrs参数,以标签属性搜索
soup.find_all(id=‘nd2‘) #返回所有标签中属性id等于nd2的标签
soup.find_all(href=re.compile("elsie"), id=‘link1‘)
#多个条件同时筛选,可用正则表达式
soup.find_all("a", class_="sister") #属性中如果有python关键字,比如class属性,不可以直接class=‘sister‘,应加个下划线与python关键字区分class_=‘sister‘
soup.find_all(attrs={"data-foo": "value"})
#类似于html5中的data-foo属性不可直接写为soup.find_all(data-foo=‘value‘),因为python命名规则中不允许有中划线(即横杠),应以字典形式传入attrs参数中,所有的属性搜索都可以使用这种方法
#(3)text参数
soup.find_all(text="Tillie") #搜索文档中的字符串内容为tillie,与name参数一样,可用列表、正则表达式等
#(4)limit参数
soup.find_all(‘a‘, limit=2) #返回搜索文档中前两个a标签,文档较大时可节约资源
#(5)recursive参数
soup.head.find_all("title", recursive=False)
#在head的直接子节点中搜索,默认为recursive=True,表示在所有子孙节点中搜索
find( name , attrs , recursive , text , **kwargs )
#与find_all用法完全一致,区别在于find只返回第一个满足条件的结果,而find_all返回的是一个列表,需遍历操作
#以下方法参数用法与 find_all() 完全相同,下面只列出区别
find_parents() &find_parent()
#find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等. find_parents() 和 find_parent() 用来搜索当前节点的父辈节点,搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容
find_next_siblings() &find_next_sibling()
#这2个方法通过 .next_siblings 属性对当 tag 的所有后面解析的兄弟 tag 节点进行迭代, find_next_siblings() 方法返回所有符合条件的后面的兄弟节点,find_next_sibling() 只返回符合条件的后面的第一个tag节点
find_previous_siblings() &find_previous_sibling()
#这2个方法通过 .previous_siblings 属性对当前 tag 的前面解析的兄弟 tag 节点进行迭代, find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点, find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点
find_all_next() &find_next()
#这2个方法通过 .next_elements 属性对当前 tag 的之后的&tag 和字符串进行迭代, find_all_next() 方法返回所有符合条件的节点, find_next() 方法返回第一个符合条件的节点
find_all_previous() 和 find_previous()
#这2个方法通过 .previous_elements 属性对当前节点前面的 tag 和字符串进行迭代, find_all_previous() 方法返回所有符合条件的节点, find_previous()方法返回第一个符合条件的节点
&标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文地址:http://www.cnblogs.com/stuqx/p/7181030.html
&&国之画&&&& &&&&chrome插件&&
版权所有 京ICP备号-2
迷上了代码!&>&Python beautifulsoup4包
Python beautifulsoup4包
上传大小:157KB
Python beautifulsoup4包 Python beautifulsoup4包Python beautifulsoup4包Python beautifulsoup4包Python beautifulsoup4包Python beautifulsoup4包
综合评分:0
下载个数:
{%username%}回复{%com_username%}{%time%}\
/*点击出现回复框*/
$(".respond_btn").on("click", function (e) {
$(this).parents(".rightLi").children(".respond_box").show();
e.stopPropagation();
$(".cancel_res").on("click", function (e) {
$(this).parents(".res_b").siblings(".res_area").val("");
$(this).parents(".respond_box").hide();
e.stopPropagation();
/*删除评论*/
$(".del_comment_c").on("click", function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_invalid/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parents(".conLi").remove();
alert(data.msg);
$(".res_btn").click(function (e) {
var parentWrap = $(this).parents(".respond_box"),
q = parentWrap.find(".form1").serializeArray(),
resStr = $.trim(parentWrap.find(".res_area_r").val());
console.log(q);
//var res_area_r = $.trim($(".res_area_r").val());
if (resStr == '') {
$(".res_text").css({color: "red"});
$.post("/index.php/comment/do_comment_reply/", q,
function (data) {
if (data.succ == 1) {
var $target,
evt = e || window.
$target = $(evt.target || evt.srcElement);
var $dd = $target.parents('dd');
var $wrapReply = $dd.find('.respond_box');
console.log($wrapReply);
//var mess = $(".res_area_r").val();
var mess = resS
var str = str.replace(/{%header%}/g, data.header)
.replace(/{%href%}/g, 'http://' + window.location.host + '/user/' + data.username)
.replace(/{%username%}/g, data.username)
.replace(/{%com_username%}/g, data.com_username)
.replace(/{%time%}/g, data.time)
.replace(/{%id%}/g, data.id)
.replace(/{%mess%}/g, mess);
$dd.after(str);
$(".respond_box").hide();
$(".res_area_r").val("");
$(".res_area").val("");
$wrapReply.hide();
alert(data.msg);
}, "json");
/*删除回复*/
$(".rightLi").on("click", '.del_comment_r', function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_comment_del/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parent().parent().parent().parent().parent().remove();
$(e.target).parents('.res_list').remove()
alert(data.msg);
//填充回复
function KeyP(v) {
var parentWrap = $(v).parents(".respond_box");
parentWrap.find(".res_area_r").val($.trim(parentWrap.find(".res_area").val()));
评论共有0条
综合评分:
积分/C币:2
大长腿的小姐姐
综合评分:
积分/C币:3
VIP会员动态
CSDN下载频道资源及相关规则调整公告V11.10
下载频道用户反馈专区
下载频道积分规则调整V1710.18
spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip
资源所需积分/C币
当前拥有积分
当前拥有C币
输入下载码
为了良好体验,不建议使用迅雷下载
Python beautifulsoup4包
会员到期时间:
剩余下载个数:
剩余积分:0
为了良好体验,不建议使用迅雷下载
积分不足!
资源所需积分/C币
当前拥有积分
您可以选择
程序员的必选
绿色安全资源
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
您的积分不足,将扣除 10 C币
为了良好体验,不建议使用迅雷下载
无法举报自己的资源
你当前的下载分为234。
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可返还被扣除的积分
被举报人:
bedbrother
举报的资源分:
请选择类型
资源无法下载 ( 404页面、下载失败、资源本身问题)
资源无法使用 (文件损坏、内容缺失、题文不符)
侵犯版权资源 (侵犯公司或个人版权)
虚假资源 (恶意欺诈、刷分资源)
含色情、危害国家安全内容
含广告、木马病毒资源
*详细原因:
Python beautifulsoup4包博主最新文章
博主热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)如何在 python 中使用 beautifulsoup4 来抓取标签中的内容? - 知乎159被浏览<strong class="NumberBoard-itemValue" title="1分享邀请回答from bs4 import BeautifulSoup
html_doc = '''
&div class="line-title"&
&span class="title"&
&span class="t-small c-green"&
(战略投资)
&span class="sechovershow jzbtn c-lined small marl10 act-ugc-edit act-ugc-edit-base1" style="margin-top:-5px"&
&i class="fa fa-pencil"&&/i&
soup = BeautifulSoup(html_doc, "html.parser")
didi = soup.b.next_element.strip()
invest = soup.b.span.next_element.strip()
didi, invest = soup.b.stripped_strings
63 条评论分享收藏感谢收起1添加评论分享收藏感谢收起python BeautifulSoup获取 网页链接的文字内容
<span type="1" blog_id="1793077" userid='
分享到朋友圈
关注作者,不错过每一篇精彩

我要回帖

更多关于 python 获取网页链接 的文章

 

随机推荐