Re: [問題] 爬蟲錯誤

看板Python作者 (用口頭禪區分年記)時間12年前 (2013/05/20 18:37), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串3/4 (看更多)
※ 引述《darklimit ()》之銘言: 應用隨機休息再繼續,還是會出現這樣的錯誤 error: [Errno 10054] 遠端主機已強制關閉一個現存的連線。 進行except例外處理,continue繼續的話 後面nameTag對應到的genre,rating 全部都會打亂 這樣應該要怎麼處理? 謝謝 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() # And Sleep Here for every connection. except HTTPError, e: #Hnalde the error, #Break, #最好在此把你處理過的資料記下來,安心上路,下次再來 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) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.160.190.62

05/19 22:30,
直覺點就遞迴防呆八..
05/19 22:30

05/20 01:09,
那應該要怎麼解決才好?
05/20 01:09

05/20 12:58,
把連線跟處理分開兩個方法~然後使用
05/20 12:58

05/20 15:38,
所以id連結到網址是一個fun,進行處理是另一個fun嗎?
05/20 15:38

05/20 15:39,
不要將連結跟處理放在同一個fun,意思是這樣嗎?
05/20 15:39

05/20 15:44,
這樣程式碼應該要怎麼修正?
05/20 15:44

05/20 15:51,
連結是因為有網址,也是因為網址才有tag進行處理
05/20 15:51

05/20 15:52,
要分開成兩個function 不太了解怎麼修正
05/20 15:52
-- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.42.51.172

05/20 18:38, , 1F
大概寫了我的想法,錯誤處理 博大精深!
05/20 18:38, 1F

05/22 17:57, , 2F
原來如此,謝謝提供的建議,學到不少~
05/22 17:57, 2F
文章代碼(AID): #1HcVpYS4 (Python)
討論串 (同標題文章)
文章代碼(AID): #1HcVpYS4 (Python)