python——beautifulsoup標簽搜索以及信息提取


原文:https://www.cnblogs.com/my1e3/p/6657926.html

 

一、 查找a標簽


(1)查找所有a標簽

復制代碼
>>> for x in soup.find_all('a'): print(x) <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>
復制代碼


(2)查找所有a標簽,且屬性值href中需要包含關鍵字“lacie

>>> for x in soup.find_all('a',href = re.compile('lacie')): print(x) <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>


(3)查找所有a標簽,且字符串內容包含關鍵字“Elsie”

>>> for x in soup.find_all('a',string = re.compile('Elsie')): print(x) <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>


(4)查找body標簽的所有子標簽,並循環打印輸出

復制代碼
>>> for x in soup.find('body').children: if isinstance(x,bs4.element.Tag): #使用isinstance過濾掉空行內容 print(x) <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 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>
復制代碼


二、信息提取(鏈接提取)


(1)解析信息標簽結構,查找所有a標簽,並提取每個a標簽中href屬性的值(即鏈接),然后存在空列表;

復制代碼
>>> linklist = [] >>> for x in soup.find_all('a'): link = x.get('href') if link: linklist.append(link) >>> for x in linklist: #驗證:環打印出linklist列表中的鏈接 print(x) http://example.com/elsie
http://example.com/lacie
http://example.com/tillie
復制代碼


小結:鏈接提取 <---> 屬性內容提取 <---> x.get('href')

(2)解析信息標簽結構,查找所有a標簽,且每個a標簽中href中包含關鍵字“elsie”,然后存入空列表中;

復制代碼
>>> linklst = [] >>> for x in soup.find_all('a', href = re.compile('elsie')): link = x.get('href') if link: linklst.append(link) >>> for x in linklst: #驗證:循環打印出linklist列表中的鏈接 print(x) http://example.com/elsie
復制代碼

小結:在進行a標簽查找時,加入了對屬性值href內容的正則匹配內容 <---> href = re.compile('elsie')

(3)解析信息標簽結構,查詢所有a標簽,然后輸出所有標簽中的“字符串”內容;

復制代碼
>>> for x in soup.find_all('a'): string = x.get_text() print(string) Elsie Lacie Tillie
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM