How to rotate proxies on a Python requests

后端 未结 2 962
予麋鹿
予麋鹿 2021-02-06 16:20

I\'m trying to do some scraping, but I get blocked every 4 requests. I have tried to change proxies but the error is the same. What should I do to change it properly?

He

相关标签:
2条回答
  • 2021-02-06 16:39

    The problem with using free proxies from sites like this is

    1. websites know about these and may block just because you're using one of them

    2. you don't know that other people haven't gotten them blacklisted by doing bad things with them

    3. the site is likely using some form of other identifier to track you across proxies based on other characteristics (device fingerprinting, proxy-piercing, etc)

    Unfortunately, there's not a lot you can do other than be more sophisticated (distribute across multiple devices, use VPN/TOR, etc) and risk your IP being blocked for attempting DDOS-like traffic or, preferably, see if the site has an API for access

    0 讨论(0)
  • 2021-02-06 16:43
    import requests
    from itertools import cycle
    
    list_proxy = ['socks5://Username:Password@IP1:20000',
                  'socks5://Username:Password@IP2:20000',
                  'socks5://Username:Password@IP3:20000',
                   'socks5://Username:Password@IP4:20000',
                  ]
    
    proxy_cycle = cycle(list_proxy)
    # Prime the pump
    proxy = next(proxy_cycle)
    
    for i in range(1, 10):
        proxy = next(proxy_cycle)
        print(proxy)
        proxies = {
          "http": proxy,
          "https":proxy
        }
        r = requests.get(url='https://ident.me/', proxies=proxies)
        print(r.text)
    
    0 讨论(0)
提交回复
热议问题