BeautifulSoup的基本用法

xiaoxiao2021-02-28  146

from bs4 import BeautifulSoup import re #一段代码 html_doc = """ <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> """ #打印html_doc所有代码 soup=BeautifulSoup(html_doc,"html.parser")#用html.parser解析器解析 print(soup.prettify()) print(soup.title) #<title>The Dormouse's story</title> print(soup.title.string) #The Dormouse's story print(soup.a) #<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a> print(soup.p) #<p class="title"><b>The Dormouse's story</b></p> print(soup.p['class']) #['title'] print(soup.findAll('a')) ''' [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] ''' for link in soup.findAll('a'): print(link.string) #Elsie #Lacie #Tillie print(soup.find(id="link3")) #<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a> print(soup.find('p')) #<p class="title"><b>The Dormouse's story</b></p> print(soup.find('p',{"class":"story"})) ''' <p class="story">Once upon a time there were three little sisters; and their names were <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a> and <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> ''' print(soup.find('p',{'class':'story'}).get_text()) ''' 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. '''

正则表达式:

for tag in soup.find_all(re.compile("t")): print(tag.name) #html #title for tag in soup.find_all(re.compile("^b")): print(tag.name) # body # b data=soup.findAll('a',href=re.compile(r"^http://example\.com/")) print(data) ''' [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] '''
转载请注明原文地址: https://www.6miu.com/read-17877.html

最新回复(0)