如何更改请求中的用户代理或VPN?

2024-06-28 11:02:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码有问题。我试图刮一页,但它给我一个“阻止”错误,我想我已经被禁止。。。。我想知道如何改变用户代理或vpn的东西,使网页不禁止我。你知道吗

   lugarDeseado="Egipto"
   paises=['ac',
   'ad',
   'ae',
   'af',
   'ag',
   'ai',
   'al',
   'am',
   'an',
   'ao',
   'aq',
   'ar',
   'es',
   'vn',
   'vu',
   'wf',
   'ws',
   'ye',
   'yt',
   'yu',
   'za',
   'zm',
   'zr',
   'zw']
    import random
    desktop_agents = ['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
             'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
             'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14',
             'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
             'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
             'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
             'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
             'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0']
try:
for i in range(len(paises)):
    ## json url ##
    url="https://www.espanol.skyscanner.com/g/browseservice/dataservices/browse/v3/bvweb/AR/ARS/es-MX/destinations/"+paises[i]+"/anywhere/anytime/?profile=minimalcityrollupwithnamesv2&include=image;holiday&apikey=8aa374f4e28e4664bf268f850f767535" 
    print(url)

    headers = {
        "referer": "https://www.espanol.skyscanner.com/transporte/vuelos-desde/buea/?adults=1&children=0&adultsv2=1&childrenv2=&infants=0&cabinclass=economy&rtn=0&preferdirects=false&outboundaltsenabled=false&inboundaltsenabled=false&ref=home",
        "user-agent" :random.choice(desktop_agents),
        "x-requested-with": "XMLHttpRequest"}
    response = requests.get(url,headers=headers)
    if response.status_code == 200:
        print ("ok")
    else:
        print ("error 400")
    data = response.json()
    try:
        for pais in data["PlacePrices"]:
            if pais.get('Name')== lugarDeseado:
                dprice = pais.get("DirectPrice")
                iprice = pais.get('IndirectPrice')

                if dprice != None and iprice != None:
                    print(f"{pais['Id']}  {dprice} {iprice}  {pais['Name']} ")
                # solo existe DirectPrice
                elif dprice == None:
                    print(f"{pais['Id']}  {iprice:<10}  {pais['Name']} ")
                # solo existe IndirectPrice
                else:
                    print(f"{pais['Id']}  {dprice:<10}  {pais['Name']} ")
            else:
                pass
    except:
        print("no")


else:
    i=i+1
except:
    pass

也许有一个图书馆,可以改变代理,vpn o任何我不知道的,谢谢 谢谢大家


Tags: urlmozillagetwindowschromesafarilikegecko