#讀取www.tsu.edu.tw網頁,如何顯示所有超連結的元素 from bs4 import BeautifulSoup as soup import urllib.request as request url = 'https://acupun.site/lecture/python/index.htm' web = request.urlopen(url) txt = web.read() print(txt) #txt轉成網頁 htm = soup(txt,'html.parser') print(htm.title) print(htm.title.name) #標題名稱:htm.title.string print(htm.title.string) #第一個<p> print(htm.p) #第一個<a> print(htm.a) #第一個<a class> print(htm.a['class']) #第一個<a class>的href print(htm.a.get('href')) #第一個<a >的innerHtml print(htm.a.string) #搜尋tag #方法1:htm.find_all('a') #方法2:htm.select('a') #顯示所有的<a> print(htm.find_all('a')) #顯示所有的<a>到innerHtml #print(htm.find_all('a')) for item in htm.find_all('a'): print(item.string) #搜尋tag #方法1:htm.find_all('a') #方法2:htm.select('a') #搜尋所有的<a> print(htm.select('a')) #搜尋 id="exp3a" print(htm.select('#exp3a')) #顯示id="exp3a的innerhtml #方法1 print(htm.select('#exp3a')[0].string) #方法2 for i in htm.select('#exp3a'): print(i.string) #搜尋第二個<a> print(htm.select('a')[1]) #搜尋第二個<a>的href print(htm.select('a')[1].get('href')) #搜尋第二個<a>的id print(htm.select('a')[1].get('id')) #搜尋第二個<a>的innerHtml print(htm.select('a')[1].string)