作者busystudent (busystudent)
看板Python
標題[問題] 網頁分析之print問題
時間Sat Apr 2 00:04:24 2016
晚安,很高興在這裡問問題,提升程式功力
2. 請看我程式碼
import requests
import re
from BeautifulSoup import BeautifulSoup
import HTMLParser
links = ['
https://goo.gl/dBtn3H']
上述的程式碼大概說明是我爬蟲了一個叫做
https://goo.gl/dBtn3H的網頁,我接下來為會擷取我想要的一段網頁內容,如下所示
#抓取使用者所收藏的標籤(未處理)
for link in links:
res = requests.get(link)
soup = BeautifulSoup(res.text.encode("utf-8"))
shop_table = soup.findAll('div',{'class':'userListTags'})
shop_table[0].findAll('a',{'href':True})
for each in shop_table:#印出所
print(each)
print each#出問題了 沒有印出全部資料
上述有兩次擷取,第一次為div.....,第二次為 shop_table[0].findAll('a',{'href':True})
我想請問當我用for迴圈( for each in shop_table:)時可以印出所有我想要的資料,但是我當我外在for迴圈外面再寫一個print each時,卻只會印印少少幾行呢?
--
Sent from my Windows
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.110.158.25
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1459526667.A.AA0.html
→ ck574b027: 第二個 each 只會是 shop_table 最後一個 element 吧 04/02 00:07
→ busystudent: 哇 等等 你可以說清楚一點嗎!! 04/02 00:08
→ busystudent: 還有我該如何處理這個問題呢? 我希望把完整版的print 04/02 00:10
→ busystudent: 存進我本機,可是一直存到不完整版那個 04/02 00:10
→ checkIO: For都跑完了 當然你print就是最後一個each值啊 04/02 09:38
→ checkIO: 因為你存的地方犯錯了吧 for迴圈不熟? 去寫個九九乘法你 04/02 09:41
→ checkIO: 就知道問題在哪了 04/02 09:41
→ octantis: 還有shop_table[0].findAll('a',{'href':True})無意義 04/02 12:22
→ octantis: 你沒用任何變數去reference它 04/02 12:23
→ MOONY135: 回答完才知道樓上已經點出他的盲點在哪了... 04/02 14:28