2013-07-27 2 views
3

기계화를 사용하여 웹 사이트에 액세스하는 코드를 작성하고 있지만 파이썬 코드를 실행할 때 종종 mechanize.ParseResponse을 사용한 행에서 무기한 중단됩니다. 그것은 나에게 오류를주는 것은 아니며 대신에 CTRL+C을 통해 중단해야합니다. 또한 메서드에 대한 올바른 인수를 사용하고 있다고 생각합니다. 그러나 나는 왜 내 프로그램이 갑자기 멈추는 지 혼란 스럽다. 어떤 생각?파이썬 : 무작위로 기계화를 기계화합니다.

추가 배경으로 Mac에서 실행 중입니다.

도움이 될 것입니다. 편집

: 나는 python bikes.py라고하고 그 다음 줄에 가끔 중단 : 다음은 내 코드

주이다 때때로

form = mechanize.ParseResponse(response, backwards_compat=False) 

, 그것은 또한에 중단됩니다 :

text = response.read() 

# bikes.py 
import re 
import webbrowser 
import mechanize 
import urllib 

brands = ["cannondale", "felt", "fuji", "giant", "specialized", "trek"] 
keywords = ["52", "53", "54", "shimano", "sora", "tiagra", "105", "ultegra", \ 
"road", "allez", "defy"] 
avoid = ["bmx", "mountain", "kids", "fixie", "jacket", "clothing", "fixed gear", \ 
"hybrid", "mtb"] 

def openLink(text): 
    text = text.lower() 
    open = False 
    for word in avoid: 
     if word in text: 
      return False 
    for word in keywords: 
     if word in text: 
      open = True 

    return open 

def scourPage(text, fileRead, fileWrite): 
    links = re.findall(r'class="row".+?href="(.+?)"', text) 

    for link in links: 
     if "http:" in link: 
      url = link 
     else: 
      url = homePage + link 

     page = urllib.urlopen(url) 
     pageText = page.read() 
     title = re.search(r'"postingtitle">.{0,10}<span.+?>[\s\'"]+(.+?)[\s\'"]{0,10}</h2>', \ 
     pageText, re.DOTALL) 
     body = re.search(r'"postingbody">(.+?)</section>', pageText, re.DOTALL) 
     openBody = False 
     openTitle = False 

     if body != None: 
      body = body.group(1) 
      openBody = openLink(body) 

     if title != None: 
      title = title.group(1) 
      openTitle = openLink(title) 

     if (openTitle and openBody) and (url not in fileRead) and (title not in fileRead): 
      fileWrite.write(title + "\n" + url + "\n") 

     fileWrite.close() 

homePage = "http://sfbay.craigslist.org" 
request = mechanize.Request(homePage) 
response = mechanize.urlopen(request) 
forms = mechanize.ParseResponse(response, backwards_compat=False) 
form = forms[0] 

request = form.click() 
response = mechanize.urlopen(request) 
emptySearch = response.geturl() 
request = mechanize.Request(emptySearch) 
response = mechanize.urlopen(request) 
forms = mechanize.ParseResponse(response, backwards_compat=False) 
form = forms[0] 

form["catAbb"] = ["bik"] 
form["maxAsk"] = "500" 
form.find_control("hasPic").items[0].selected = True 

for brand in brands: 
    form["query"] = brand 

    request = form.click() 
    response = mechanize.urlopen(request) 
    text = response.read() 

    fileR = open('bikes.txt', 'r').read() 
    fileA = open('bikes.txt', 'a') 

    scourPage(text, fileR, fileA) 

    fileA.close() 

    next = re.findall(r'class="nplink next".{0,50}<a href=\'(.+?)\'>', text, re.DOTALL) 

    while len(next) != 0: 
     text = urllib.urlopen(next[0]).read() 

     fileR = open('bikes.txt', 'r').read() 
     fileA = open('bikes.txt', 'a') 

     scourPage(text, fileR, fileA) 

     fileA.close() 

     next = re.findall(r'class="nplink next".{0,50}<a href=\'(.+?)\'>', text, re.DOTALL) 

이 코드는 Craigslist 광고를 통해 내가 원하지 않는 광고를 제거하려고 시도합니다. 이 경우 도로 자전거를 찾고 산악 자전거 및 기타 목록을 피하려고합니다.

는 UPDATE :

꽤 오랜 시간을 기다린 후에, 나는 마지막으로 키보드를 다시 실행을 중단하고는 form = mechanize.ParseResponse(response, backwards_compat=False) 줄에서 멈췄다. 나는 다시 그것을 실행하려고이 오류가 발생했습니다 : 귀하의 while 루프 동작을 설명하는 무한 갈 수

Traceback (most recent call last): 
    File "bikes.py", line 97, in <module> 
    forms = mechanize.ParseResponse(response, backwards_compat=False) 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_form.py", line 945, in ParseResponse 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_form.py", line 981, in _ParseFileEx 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_form.py", line 758, in feed 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_sgmllib_copy.py", line 110, in feed 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_sgmllib_copy.py", line 192, in goahead 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_form.py", line 654, in handle_charref 
    File "build/bdist.macosx-10.8-intel/egg/mechanize/_form.py", line 149, in unescape_charref 
ValueError: unichr() arg not in range(0x10000) (narrow Python build) 
+0

코드를 제공해 주실 수 있습니까? – svineet

+0

추가됨. 희망이 도움이됩니다. : X – Zhouster

답변

0

. 확인하지 않았 니?

코드가 CTRL-C 일 때 런타임 오류가 발생해도 반드시 코드가 깨 졌음을 의미하지는 않습니다.

+0

for 문에 print 문을 넣었습니다. "text = response.read()"줄에서 멈추지 않습니다. while 루프는 무한하다면 많은 문장을 출력 할 것이기 때문에 while 루프는 괜찮은 것으로 확신합니다.하지만 그렇지 않습니다. 나는 그것이 urllib와 관련이 있다고 생각하지만 그것은 단지 추측이다. – Zhouster

관련 문제