【问题标题】:How to check if my python proxy rotator and user agent spoofing works?如何检查我的 python 代理旋转器和用户代理欺骗是否有效?
【发布时间】:2018-09-03 03:13:19
【问题描述】:

我已经编写了代理 IP 轮换和用户代理欺骗的代码,以便在抓取中使用。但是由于提供了代码作为示例,当我将它添加到我的代码中时,我不知道它是否真的有效。

我是 Python 的初学者。我只是将它添加到我的 .py 文件中(在用于抓取的代码之后)。当我添加它并开始抓取它时,它可以工作并获取所有数据,但我不知道它是否正常工作。

  1. 我是否必须为这些代码创建另一个文件(用户代理欺骗和 IP 轮换)?
  2. 当我进行抓取时,我如何知道这些是否有效?
  3. 他们是否定义了 url 是否重要?

代理轮换:

    from lxml.html import fromstring
    import requests
    from itertools import cycle
    import traceback

proxies = ['121.129.127.209:80', '124.41.215.238:45169', '185.93.3.123:8080', '194.182.64.67:3128', '106.0.38.174:8080', '163.172.175.210:3128', '13.92.196.150:8080']
    proxies = get_proxies()
    proxy_pool = cycle(proxies)

url = 'https://httpbin.org/ip'
for i in range(1,11):
    proxy = next(proxy_pool)
    print("Request #%d"%i)
    try:
        response = requests.get(url,proxies={"http": proxy, "https": proxy})
        print(response.json())
    except:
        print("Skipping. Connnection error")

用户代理欺骗:

    import requests
import random
user_agent_list = [
   #Chrome
    'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
    'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
    #Firefox
    'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)',
    'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)',
    'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)'
]
url = 'https://httpbin.org/user-agent'
#Lets make 5 requests and see what user agents are used 

#Using Requests 
for i in range(1,6):
    #Pick a random user agent
    user_agent = random.choice(user_agent_list)
    #Set the headers 
    headers = {'User-Agent': user_agent}
    #Make the request
    response = requests.get(url,headers=headers)

    print("Request #%d\nUser-Agent Sent:%s\nUser Agent Recevied by HTTPBin:"%(i,user_agent))
    print(response.content)
    print("-------------------\n\n")

【问题讨论】:

    标签: python-3.x proxy ip user-agent


    【解决方案1】:

    如果您想检查您的代理和用户代理是否在轮换,您需要访问请求 bin 网站,激活一个端点并在您的 python 代码中使用该端点来代替之前请求的内容。

    然后,您将检查请求箱并阅读执行 python 代码后现在列出的 Get 请求的用户代理和 IP 地址的说明。

    【讨论】:

      【解决方案2】:

      我建议运行大量请求,而不是尝试可视化您获得的 IP 分布。您可以在控制台中使用 for 循环和背景 curl 命令轻松执行此操作:请参阅 https://weautomate.org/articles/load-testing-ip-rotation-proxy/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-01
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-22
        • 2015-12-31
        相关资源
        最近更新 更多