python3 BeautifulSoup模塊使用字典的方法抓取a標簽內的數據示例
更新時間:2019年11月28日 09:08:24 作者:weixin_34351321
這篇文章主要介紹了python3 BeautifulSoup模塊使用字典的方法抓取a標簽內的數據,結合實例形式Fenix了python3 BeautifulSoup模塊進行數據的抓取相關操作技巧,需要的朋友可以參考下
本文實例講述了python3 BeautifulSoup模塊使用字典的方法抓取a標簽內的數據。分享給大家供大家參考,具體如下:
# -*- coding:utf-8 -*- #python 2.7 #XiaoDeng #http://tieba.baidu.com/p/2460150866 #標簽操作 from bs4 import BeautifulSoup import urllib.request import re #如果是網址,可以用這個辦法來讀取網頁 #html_doc = "http://tieba.baidu.com/p/2460150866" #req = urllib.request.Request(html_doc) #webpage = urllib.request.urlopen(req) #html = webpage.read() html=""" <html><head><title>The Dormouse's story</title></head> <body> <p class="title" name="dromouse"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a rel="external nofollow" rel="external nofollow" class="sister" id="xiaodeng"><!-- Elsie --></a>, <a rel="external nofollow" rel="external nofollow" class="sister" id="link2">Lacie</a> and <a rel="external nofollow" class="sister" id="link3">Tillie</a>; <a rel="external nofollow" rel="external nofollow" class="sister" id="xiaodeng">Lacie</a> and they lived at the bottom of a well.</p> <p class="story">...</p> """ soup = BeautifulSoup(html, 'html.parser') #文檔對象 #查找a標簽,只會查找出一個a標簽 #print(soup.a)#<a class="sister" rel="external nofollow" rel="external nofollow" id="xiaodeng"><!-- Elsie --></a> for k in soup.find_all('a'): print(k) print(k['class'])#查a標簽的class屬性 print(k['id'])#查a標簽的id值 print(k['href'])#查a標簽的href值 print(k.string)#查a標簽的string #如果,標簽中含有其他標簽,比如..,此時要提取中的數據,需要用k.get_text() #tag.get('calss'),也可以達到這個效果 Python Socket編程技巧總結》、《Python正則表達式用法總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
更多關于Python相關內容可查看本站專題:《
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python如何快速生成本項目的requeirments.txt實現
本文主要介紹了Python如何快速生成本項目的requeirments.txt實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03Python實現定時備份mysql數據庫并把備份數據庫郵件發(fā)送
這篇文章主要介紹了Python實現定時備份mysql數據庫并把備份數據庫郵件發(fā)送的相關資料,需要的朋友可以參考下2018-03-03