python3爬虫03(find_all用法等)

news/2024/7/19 12:02:32 标签: python, 爬虫
#read1.html文件
# <html><head><title>The Dormouse's story</title></head>
# <body>
# <p class="title"><b>The Dormouse's story</b></p>
#
# <p class="story">Once upon a time there were three little sisters; and their names were
# <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
# <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
# <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
# and they lived at the bottom of a well.</p>
#
# <p class="story">...</p></body></html>


#!/usr/bin/env python
# # -*- coding:UTF-8 -*-

import os
import re
import requests
from bs4 import NavigableString
from bs4 import BeautifulSoup

curpath=os.path.dirname(os.path.realpath(__file__))
hmtlpath=os.path.join(curpath,'read1.html')

res=requests.get(hmtlpath)

soup=BeautifulSoup(res.content,features="html.parser")

for str in soup.stripped_strings:
print(repr(str))

links=soup.find_all(class_="sister")
for parent in links.parents:
if parent is None:
print(parent)
else:
print(parent.name)

print(links.next_sibling)

for link in links:
print(link.next_element)
print(link.next_sibling)

print(link.privous_element)
print(link.privous_sibling)

def has_class_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id')

def not_lacie(href):
return href and not re.compile("lacie").search(href)

def not_tillie(href):
return href and not re.compile("tillie").search(href)

def not_tillie1(id):
return id and not re.compile("link2").search(id)

file=open("soup.html","r",encoding="utf-8")
soup=BeautifulSoup(file,features="lxml")

#find_all用法
tags=soup.find_all(re.compile('^b'))
tags=soup.find_all('b')
tags=soup.find_all(['a','b'])
tags=soup.find_all(has_class_no_id)
tags=soup.find_all(True)
tags=soup.find_all(href=not_lacie)
for tag in tags:
print(tag.name)

def surrounded_by_strings(tag):
return (isinstance(tag.next_element, NavigableString)
and isinstance(tag.previous_element, NavigableString))

tags=soup.find_all(id=not_tillie1)
for tag in tags:
print(tag)

tags=soup.find_all(attrs={"id":"link3"})
for tag in tags:
print(tag)

soup.find_all(recursive=False)
tags=soup.select("body a")
tags=soup.select("p > a")
tags=soup.select("p > #link1")
tags=soup.select("html head title")
tags=soup.select(".sister")
tags=soup.select("[class~=sister]")
tags=soup.select("#link1 + .sister")
tags=soup.select("#link1")
tags=soup.select("a#link1")
tags=soup.select("a[href]")
tags=soup.select('a[href^="http://example"]')
tags=soup.select('a[href$="tillie"]')
tags=soup.select('a[href*=".com/el"]')
for tag in tags:
print(tag)

file=open("soup.html","r",encoding="utf-8")
soup=BeautifulSoup(file,features="html.parser")
soup=BeautifulSoup(file,features="html.parser")
print(soup.prettify())
print(type(soup))
print(type(soup.title))
print(type(soup.title.string))
print(type(soup.b.string))

print(soup.head.name)
print(soup.title.name)
print(soup.a.name)
print(soup.name)

tag=soup.a
print(tag["href"])
print(tag.string)
print(tag["class"])
print(tag.attrs)

print(soup.title.string)
print(soup.title.name)
print(soup.p.attrs)
print(soup.a.attrs)
print(soup.a["class"])

转载于:https://www.cnblogs.com/NiceTime/p/10070218.html


http://www.niftyadmin.cn/n/687764.html

相关文章

希赛和51cto哪个好_软考哪个好?哪个更好考?答案在这!

希赛软考之家关注我们&#xff0c;顺利取证▲点击蓝字关注&#xff0c;获取更多资讯软考有初、中、高三个级别&#xff0c;每个级别又有多个资格考试项目&#xff0c;因此很多考生在报名的时候不知道到底该报考哪个级别的资格&#xff0c; 那么到底考哪个好呢&#xff1f;◆ ◆…

python基础-修改haproxy配置文件

需要掌握的知识&#xff1a; 1、函数 2、文件处理 3、tag的用法 4、程序的解耦 需求&#xff1a; 1&#xff1a;查询 2&#xff1a;添加 3&#xff1a;删除 4&#xff1a;修改 5&#xff1a;退出 haproxy.conf 配置文件内容&#xff1a; 1 global2 log 127.0.0.1 local…

python中常见单词意思_Python中经常使用的单词

您可以先定义一个函数来获取字符串中的所有k-mer&#xff1a;def get_all_k_mer(string, k1):length len(string)return [string[i: i k] for i in xrange(length-k1)]然后你可以使用collections.Counter来计算每个k-mer的重复次数&#xff1a;>>> from collections …

linux运维基础篇 unit12

12.不同系统之间的文件传输1.文件归档1.文件归档&#xff0c;就是把多个文件变成一个归档文件2.tarc #创建f ##指定归档文件名称t ##显示归档文件中的内容r ##向归档文件中添加文件--ge…

webControls与客户端脚本路径

网上有用的资料不多&#xff0c;在一本电子书中摘抄了内容如下 webControls配置节只有一个clientScriptsLocation属性&#xff0c;此属性用于指定ASP.NET客户端脚本的默认存放路径。这些文件是包含在HTML代码生成的ASPX页面时这些需要的客户端功能&#xff0c;如智能导航和客户…

python3基础04(requests常见请求)

#!/usr/bin/env python# -*- coding:utf-8 -*- import requestsimport jsonimport reimport urllib3from urllib.parse import urlencode,quote,unquoteurl"https://www.baidu.com"resrequests.get(url)#响应内容常见字段信息print(res.text)print(res.content)print…

dede织梦技巧:教你彻底解决dede按权重排序的问题(转)

dede排序对网站来说一直存在问题&#xff0c;默认是按照最新发布时间排序。这样排序有个问题&#xff0c;一旦更新之后即被视为最新发布&#xff0c;于是原本做好的排序瞬间就乱了。这种时候&#xff0c;按权重排序是个很好的选择&#xff0c;但按权重排序到处存在BUG~。很多地…

css3 all属性

ie不支持&#xff0c;谷歌火狐支持&#xff0c;safari9支持&#xff0c;移动端高版本支持 all属性实际上是所有CSS属性的缩写&#xff0c;表示&#xff0c;所有的CSS属性都怎样怎样&#xff0c;但是&#xff0c;不包括unicode-bidi和direction这两个CSS属性。 我们可能知道&…