[問題] bs4抓取資料問題

看板Python作者 (談無慾)時間8年前 (2015/07/29 12:18), 編輯推噓0(005)
留言5則, 2人參與, 最新討論串1/1
我想從網頁版的ptt抓取資料 每篇文章的 作者跟發文日 還有文章的網頁 import requests from bs4 import BeautifulSoup import sys res_index = requests.get("https://www.ptt.cc/bbs/gamesale/index.html") soup_index = BeautifulSoup(res_index.text,"html.parser") #抓每篇文章的URL聯結 main_container_index = soup_index.select('.r-ent') for link in main_container_index: print(link.select('div.author')[0].text, link.select('div.date')[0].text) print(link.find('a')['href']) 我有疑問的是print(link.find('a')['href'])這行 因為我想要抓網址 但一定要這樣寫才可以抓到 a href="/bbs/Gamesale/M.1438136421.A.732.html" 這行 不知道大家可以幫我解釋一下為甚麼要這樣寫嗎 =================以下是網頁長相 thireh 7/29 <div class="title"> <a href="/bbs/Gamesale/M.1438136421.A.732.html">[PC ] 售mycard 點數85折</a> </div> DREAMLS 7/29 <div class="title"> <a href="/bbs/Gamesale/M.1438137518.A.6A3.html">[PSV ] 售/換 psv2007(青檸白) +16g記憶卡+六片超值遊戲</a> </div> CTC0115 7/29 <div class="title"> <a href="/bbs/Gamesale/M.1438137532.A.B0E.html">[PS3 ] 售 VR快打5 </a> </div> -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.123.103 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1438143508.A.997.html

08/01 17:02, , 1F
先了解 html可以依照他的tag (<a></a>,<div></div>這些)
08/01 17:02, 1F

08/01 17:02, , 2F
可以被解讀成一個樹狀結構,而bs4就是幫你把建樹和搜尋
08/01 17:02, 2F

08/01 17:03, , 3F
整合在一起的一個package。
08/01 17:03, 3F

08/01 18:32, , 4F
https://goo.gl/lCmf4C 幫你"逐行"解釋了 耐心看吧
08/01 18:32, 4F

08/02 21:24, , 5F
感恩
08/02 21:24, 5F
文章代碼(AID): #1Lk5GKcN (Python)