Re: [問題] bs4抓取連結問題

看板Python作者 (談無慾)時間8年前 (2015/08/07 18:28), 8年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《nendi (淡定~)》之銘言: : 您好,我是一個剛學一個月的新手,有些指令還不太熟悉 : 我想抓取某個網頁的超連結 : 其中我要的連結都在 <ul class="no_listyle ar_grd"> 之下 : 我的程式碼如下 : from bs4 import BeautifulSoup : import requests : res= requests.get('http: xxx') : #實際網址太長,我用縮網址發文被說是廣告,只好寫xxx代替 : soup = BeautifulSoup(res.text) : ul = soup.findAll('ul',{'class':"no_listyle ar_grd"}) : #找出來後,我想把<a href=" ">之中的超連結取出來,所以我試接著寫 : for link in ul: : print(link.find('a')['href']) : 但只顯現出了第一個連結,我希望能把所有超連結取出 : 可否哪位好心大大指導一下呢? : 感謝 res_index = requests.get(xxxx) soup_index = BeautifulSoup(res_index.text,"html.parser") main_container_index = soup_index.select('.r-ent') for link in main_container_index: try: Post_link = link.find('a')['href'] 我每取出一筆就直接寫入csv檔這樣 可以參考一下 或者參考 ======================================================= 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 = soup_index.find_all('a', href=True) href = main_container[7].get('href')[19:23] print(type(href)) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.177.187.24 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1438943324.A.9C0.html ※ 編輯: MOONY135 (180.177.187.24), 08/07/2015 18:32:00
文章代碼(AID): #1Ln8XSd0 (Python)
文章代碼(AID): #1Ln8XSd0 (Python)