基於bs4的網頁遊歷

來源:互聯網
上載者:User

標籤:page   parse   his   tor   list   一個   png   網頁   port   

1. HTML的基本格式

<html> <head>  <title>   This is a python demo page  </title> </head> <body>  <p class="title">   <b>    The demo python introduces several python courses.   </b>  </p>  <p class="course">   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:   <a class="py1" href="http://www.icourse163.org/course/BIT-268001" id="link1">    Basic Python   </a>   and   <a class="py2" href="http://www.icourse163.org/course/BIT-1001870001" id="link2">    Advanced Python   </a>

1. 下行遊歷。

1.1 contents

import requests                     #r = requests.get("http://python123.io/ws/demo.html")demo = r.textfrom bs4 import BeautifulSoupsoup = BeautifulSoup(demo,"html.parser")print(soup.body.contents)      # 返回所有子節點的資訊。。print(soup.body.contents[1])   # 以列表的形勢,因此可以進行列表的處理


print(soup.p.contents) # 只返回標籤的全部子第一個資訊


 

1.2 氣死我了連續兩次都沒儲存上。。。。。

children and descendants

print(soup.body.children)       # 僅迴圈                      # <list_iterator object at 0x01383D10>print(soup.body.descendants)    # 僅迴圈                     # <generator object descendants at 0x024B42A0>for i in soup.body.children:    print(i)for j in soup.body.descendants:    print(j)

2.  上行遊歷

2.1 parent

print(soup.a.parents)
print(soup.p.parent)

2.2 parents

for i in soup.p.parents:    print(i.prettify())

3. 平行遊歷(僅同一父親節點下)

基於bs4的網頁遊歷

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.