Re: [問題] 爬蟲錯誤

看板Python作者 (用口頭禪區分年記)時間12年前 (2013/05/20 00:48), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串2/4 (看更多)
※ 引述《darklimit ()》之銘言: : 應用隨機休息再繼續,還是會出現這樣的錯誤 : error: [Errno 10054] 遠端主機已強制關閉一個現存的連線。 : 進行except例外處理,continue繼續的話 : 後面nameTag對應到的genre,rating 全部都會打亂 : 這樣應該要怎麼處理? : 謝謝 idlist 是什麼? 給個小一點的例子吧。 : for i in idlist: : headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT : 6.1;en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} : req = : urllib2.Request("http://www.imdb.com/title/tt"+i+"/",headers=headers) : try: : html = : urllib2.urlopen("http://www.imdb.com/title/tt"+i+"/",timeout = 30) : htmls = html.read() : html.close : soup = BeautifulSoup(htmls) : nameTag = [a.get_text() for a in soup.find_all("title")] : genreTag = [a.get_text() for a in : soup.find_all("span",{"itemprop":"genre"})] : ratingTag = soup.find_all("span",{"itemprop":"ratingValue"}) : for tag in nameTag: : titlelist.append(nameTag) : for tag in genreTag: : genrelist.append(genreTag) : break : for tag in ratingTag: : val = ''.join(tag.find(text=True)) : valuelist.append(val) : except HTTPError, e: : print e.code : print e.read() : #continue : except URLError, e: : print 'Reason: ', e.reason : #continue : rsleep = random.randint(10, 40) : time.sleep(rsleep) : return zip(titlelist, genrelist, valuelist) 你的 Try Except 的區塊太大了,或許要重構一下。 要確定程式真的把資料抓到手了,再分析 而且感覺你貼上以後縮排跑掉了,而python 的縮排是有意義的。 這樣沒有辦法知道你確切想表達的意思。 下次這麼長的Code, 可以同時附上一個Gist 的 link -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.62.8.191 ※ 編輯: timTan 來自: 61.62.8.191 (05/20 01:11)

05/20 15:30, , 1F
解決方式原文有人推了~ 連線 處理文字。 要分開!
05/20 15:30, 1F

05/20 15:41, , 2F
不要像我寫的在同一個fun裡,就能避免中斷情況了嗎?
05/20 15:41, 2F
文章代碼(AID): #1HcG9VLB (Python)
討論串 (同標題文章)
本文引述了以下文章的的內容:
0
8
完整討論串 (本文為第 2 之 4 篇):
0
8
文章代碼(AID): #1HcG9VLB (Python)