作者timTan (用口头禅区分年记)
看板Python
标题Re: [问题] 爬虫错误
时间Mon May 20 00:48:30 2013
※ 引述《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)
1F:→ timTan:解决方式原文有人推了~ 连线 处理文字。 要分开! 05/20 15:30
2F:→ darklimit:不要像我写的在同一个fun里,就能避免中断情况了吗? 05/20 15:41