2017-11-06 3 views
0

내가 긁어 내고 싶은 웹 사이트가 있다고 가정 해 봅시다. 전의. cheapoair.com파이썬에서 프록시 전환 : Psuedo code

파이썬에서 일반적인 요청을 사용하여 첫 번째 가상 페이지의 데이터를 긁어 내고 싶습니다. 서버에 의해 막히면 결국 프록시로 전환하고 싶습니다. 프록시 서버 목록과 메소드가 있으며 사용자 에이전트 문자열 목록도 있습니다. 그러나 나는 문제를 생각하는 데 도움이 필요하다고 생각합니다. 내가 찾고 있어요 무엇

import requests 
from proxy_def import * 
from http import cookiejar 
import time 
from socket import error as SocketError 
import sys 

start_time = time.time() 


class BlockAll(cookiejar.CookiePolicy): 
    return_ok = set_ok = domain_return_ok = path_return_ok = lambda self, *args, **kwargs: False 
    netscape = True 
    rfc2965 = hide_cookie2 = False 


headers = {'User-Agent': uagen()} 

print(headers) 

s = requests.Session() 
s.cookies.set_policy(BlockAll) 
cookies = {'SetCurrency': 'USD'} 
sp = proxit() 
for i in range(100000000000): 
    while True: 
     try: 
      print('trying on ', sp) 
      print('with user agent headers', headers) 
      s.proxies = {"http": sp} 
      r = s.get("http://www.cheapoair.com", headers=headers, timeout=15, cookies=cookies) 
      print(i, sp, 'success') 
      print("--- %s seconds ---" % (time.time() - start_time)) 
     except SocketError as e: 
      print('passing ', sp) 
      sp = proxit() 
      headers = {'User-Agent': uagen()} 
      print('this is the new proxy ', sp) 
      print('this is the new headers ', headers) 
      continue 
     except requests.ConnectionError as e: 
      print('passing ', sp) 
      sp = proxit() 
      headers = {'User-Agent': uagen()} 
      print('this is the new proxy ', sp) 
      print('this is the new headers ', headers) 
      continue 
     except requests.Timeout as e: 
      print('passing ', sp) 
      sp = proxit() 
      headers = {'User-Agent': uagen()} 
      print('this is the new proxy ', sp) 
      print('this is the new headers ', headers) 
      continue 
     except KeyboardInterrupt: 
      print("The program has been terminated") 
      sys.exit(1) 
     break 

#print(r.text) 
print('all done', 
     '\n') 

:

참조 uagen를 들어

()는 프록시 여기

를 반환) 사용자 에이전트 문자열을

proxit (나는 지금까지 무엇을 가지고 돌아갑니다 for는 정상적인 요청 (프록시가 아닌)으로 시작하는 방법, 서버 (예 : 서버에 의해 거부 됨)와 같은 오류로 끝나는 경우 프록시로 전환 한 후 다시 시도하는 방법에 대한 아이디어입니다.

나는 거의 그 그림을 그릴 수 있지만, 그것을 볼 수는 없다.

것은 내가

for i in range(1000000000000):

후 변수를 배치하지만 while true: 전에 즉, sp를 업데이트하는 경우 다음이 작동 할 수도 있다는 생각 해요. 또 다른 가능성은 아마도 s.proxies = {"http": ""}을 선언 한 다음 오류가 발생하면 s.poxies = {"http": "proxit()"} 또는 s.poxies = {"http": "sp"}

으로 전환하십시오. 고마워요!

답변

1

나는 그것을 알아 냈다.

while True: 
    try: 
     #do this thing 
     #but remove variable from here and declare it before "while True" 
    except SockerError as e: 
     #switch headers, switch user agent string 
     s.proxies = {"http": proxit()} 
     continue 
는 서버에서 오류를 가져옵니다 후 변수를 새로 고쳐집니다

관련 문제