BeautifulSoup同一父標簽下相同子標簽爬取



<div id="content-innerText">
  <p>
    <img src="http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-51.jpg" title="" alt="" width="801" height="1200">
    <img src="http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-52.jpg" title="" alt="" width="1080" height="1616">
    <img src="http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-48.jpg" title="" alt="" width="682" height="1024">
    <img src="http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-49.jpg" title="" alt="" width="682" height="1024">
    <img src="http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-491.jpg" title="" alt="" width="682" height="1024">
    <img src="http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-50.jpg" title="" alt="" width="682" height="1024">
  </p>
</div>


  • 爬取 img 標簽中的 src 屬性
elem = soup.find_all("div", {"id": "content-innerText"})
for i in elem[0].p.children:
    print(i['src'])
for i in soup.find_all("div", {"id": "content-innerText"})[0].p.children:
    print(i['src'])
  • 結果:
http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-51.jpg
http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-52.jpg
http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-48.jpg
http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-49.jpg
http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-491.jpg
http://222.186.150.182/wp-content/uploads/2016/02/2016-10-30_22-28-50.jpg





35.jpg
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標準。 注意:講述HT...
    kismetajun閱讀 27,695評論 1 45
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,923評論 18 139
  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標簽默認的外補...
    _Yfling閱讀 13,796評論 1 92
  • HTML 5 HTML5概述 因特網上的信息是以網頁的形式展示給用戶的,因此網頁是網絡信息傳遞的載體。網頁文件是用...
    阿啊阿吖丁閱讀 4,068評論 0 0
  • 0. 前言 在介紹BeautifulSoup模塊前, 我們先分析一下我們要爬取的網頁結構是什么樣的。通常網頁都包含...
    查德筆記閱讀 6,461評論 0 4