看板 Python 關於我們 聯絡資訊
應用隨機休息再繼續,還是會出現這樣的錯誤 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 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
qwertmn:直覺點就遞迴防呆八.. 05/19 22:30
darklimit:那應該要怎麼解決才好? 05/20 01:09
swpoker:把連線跟處理分開兩個方法~然後使用 05/20 12:58
darklimit:所以id連結到網址是一個fun,進行處理是另一個fun嗎? 05/20 15:38
darklimit:不要將連結跟處理放在同一個fun,意思是這樣嗎? 05/20 15:39
darklimit:這樣程式碼應該要怎麼修正? 05/20 15:44
darklimit:連結是因為有網址,也是因為網址才有tag進行處理 05/20 15:51
darklimit:要分開成兩個function 不太了解怎麼修正 05/20 15:52