看板 Python 關於我們 聯絡資訊
※ 引述《busystudent (busystudent)》之銘言: : a主程式碼 : http://dpaste.com/2S5SC2B 你主程式碼下面有問題 links = ['http://www.diigo.com/user/tony_swann/' + a ]放到外面去 否則只有一個然後包成list再用for進去list裡讀出來有何意義? 改成: links = ['http://www.diigo.com/user/tony_swann/' + x for x in tag_list] for link in links: res = requests.get(link) soup = BeautifulSoup(res.text.encode("utf-8"), 'html.parser') fol_table = soup.findAll('div', {'class': 'Titleinner'}) a_links = [tag['href'] for link in fol_table for tag in link.findAll('a', {'href': True})] #將一個tag的所有結果放進同一個list,然後將list丟給b主程式碼 可簡化成a_links = [a['href'] for a in soup.select('div.Titleinner a')] 將fol_table與a_link合併 結構: links裡裝所有的標籤 a_link為一個標籤裡的所有分析到的網址 然後把a_link丟進b主程式碼 : b主程式碼 : http://dpaste.com/1A5ZHM8 不知道是不是你要的答案,你的問題我要想很久才猜得到你"可能"想表達的意思... 列出你想得到的答案型式,不然光猜就要猜好久 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.164.133.245 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1461273344.A.39E.html ※ 編輯: octantis (1.164.133.245), 04/22/2016 06:18:04
MOONY135: 看描述就覺得好亂喔... 04/22 10:24
busystudent: 你好 謝謝你的回文,之前的敘述不好,不好意思 04/22 12:01
busystudent: 我把文章敘述修改了 04/22 12:02
busystudent: 你好 有一行我看不懂,想請你解釋 04/22 16:39
busystudent: a_links = [tag['href'] for link in fol_table 04/22 16:41
busystudent: for tag in link.findAll('a', {'hr 04/22 16:41
busystudent: ef': True})] 04/22 16:41
busystudent: 這一行是什麼意思? 04/22 16:41
busystudent: 我第一看到撰寫兩種for在一行的式子 04/22 16:42
octantis: 就是相當於for迴圈裡再一個for迴圈 04/22 17:03
octantis: 不然就是用下面寫的簡化版 看起來比較乾淨簡單 04/22 17:04
octantis: 那行等於 04/22 17:05
octantis: for link in fol_table: 04/22 17:06
octantis: for tag in link.findAll('a', {'href': True}): 04/22 17:07
octantis: a_links.append(tag['href']) 04/22 17:08
busystudent: 謝謝你的說明! 04/22 17:09
busystudent: 還有簡化版那行一否有誤,它報錯'NoneType' 04/22 17:10
busystudent: 執行如下 http://dpaste.com/196AJ7F 04/22 17:12
octantis: 啊!你是用bs3而非bs4,難怪會出錯,bs3可能沒法這樣用 04/22 20:14