2016-06-30 4 views
1

나는 을 모두 here에 시도했지만 여전히 urllib.error.HTTPError: HTTP Error 400: Bad Request이 표시됩니다. 나는 또한 this을 시도했지만, urllib.error.URLError: File Not Found이 나옵니다. 무엇을해야할지 모르겠다. 나의 현재 코드는있다.Urllib 나쁜 요청 문제

from bs4 import BeautifulSoup 
import urllib.request,json,ast 

with open ("urller.json") as f: 
    cc = json.load(f) #the file I get links, you can try this link instead of this 
    #cc = ../games/index.php?g_id=23521&game=0RBITALIS 

for x in ast.literal_eval(cc): #cc is a str(list) so I have to convert 
    if x.startswith("../"): 

     r = urllib.request.Request("http://www.game-debate.com{}".format(x[2::]),headers={'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'}) 
     #x[2::] because I removed '../' parts from urlls 

     rr = urllib.request.urlopen(r).read() 
     soup = BeautifulSoup(rr) 

     for y in soup.find_all("ul",attrs={'class':['devDefSysReqList']}): 
      print (y.text) 

편집 : 당신은 내가 오류를 6 링크에서 모든 시간을 얻을 수 있기 때문에 아마, 어떤 오류가 표시되지 않습니다 만 1 링크를 시도하십시오.

+0

'urllib'을 (를) 사용 하시겠습니까? 난 그냥'requests.get ("http://www.game-debate.com/games/index.php?g_id=23521&game=0RBITALIS")'시도하고 완벽하게 작동합니다. '요청'은 사실상 모든면에서 훨씬 우월합니다. –

+0

@AkshatMahajan하지만 json 파일의 여섯 번째 링크에서 매회 나쁜 요청 오류가 발생하기 때문에 아마도 1 개의 링크 만 시도하면 질문이 편집됩니다. – GLHF

+0

요청하기 전에 각 URL을 인쇄 해 보았습니까? 아마도 URL은 명백한 형식으로 잘못 작성되었을 수도 있습니다. –

답변

1

빠른 수정 +과 공간을 대체하는 것입니다 :

from bs4 import BeautifulSoup 
import urllib.request,json,ast 
from urllib.parse import quote, urljoin 

with open ("urller.json") as f: 
    cc = json.load(f) #the file I get links, you can try this link instead of this 
    url = "http://www.game-debate.com" 


    for x in ast.literal_eval(cc): # cc is a str(list) so I have to convert 
     if x.startswith("../"): 
      r = urllib.request.Request(urljoin(url, quote(x.lstrip("."))), headers={ 
       'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'}) 

      rr = urllib.request.urlopen(r).read() 
      soup = BeautifulSoup(rr) 
      print(rr.decode("utf-8")) 

      for y in soup.find_all("ul", attrs={'class':['devDefSysReqList']}): 
       print (y.text) 

스페이스의 URL에 유효하고 필요하지 않은 :

url = "http://www.game-debate.com" 
r = urllib.request.Request(url + x[2:] ,headers={'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11'}) 

더 나은 옵션은 PARAMS는 quote을 URLLIB 수 있도록 할 수있다 %20으로 인코딩 된 백분율로 표시되거나 +으로 대체됩니다.