【问题标题】:how to apply mutli-threading for getting working URLs from list of 1000 URLs - Python如何应用多线程从 1000 个 URL 列表中获取工作 URL - Python
【发布时间】:2020-06-16 07:46:57
【问题描述】:

通常检查 1000 个 URL 的状态代码需要 9 小时 30 分钟 如何为这些 URL 应用多线程,我的输出应该是工作 URL,其状态代码为 200。 例如,在 100 个 URL 中,我们有 70 个带有 200 个代码,其余的是 404 或其他任何内容。

输入 = ['https://xxxxxx1','https://xxxxxx2',........,'https://xxxxxx100']

输出:- ['https://xxxxxx1','https://xxxxxx2','https://xxxxxx3',........,'https://xxxxxx70'] 这些将有 200 个状态码

【问题讨论】:

标签: python-3.x web-scraping python-requests


【解决方案1】:

只是一个简单的线程在 python 中工作的建议。您可以使用将您的 url 列表一分为二,然后创建两个函数,它们在两个单独的线程上运行。

import threading 
Output = []
List1 = [half of your urls]
List2 = [other half of your urls]  
def check_status(lst): 
    """
    Do you task
    """

def check_status(lst): 
    """
    Do you task
    """

if __name__ == "__main__": 
    # creating thread 
    t1 = threading.Thread(target=check_status, args=(List1,)) 
    t2 = threading.Thread(target=check_status_2, args=(List2,)) 

    # starting thread 1 
    t1.start() 
    # starting thread 2 
    t2.start() 

    # wait until thread 1 is completely executed 
    t1.join() 
    # wait until thread 2 is completely executed 
    t2.join() 

    # both threads completely executed 
    print("Completed") 

一旦线程启动,您的程序也会继续执行。为了在线程完成之前停止执行正在进行的程序,请使用 join 方法。将给出 200 状态代码的 url 附加到 Output

【讨论】:

  • 如何将 200 状态的 url 添加到输出列表中?
  • 使用 requests 库点击 url 并检查它们的状态代码。如果返回 200,请执行 Output.append(url)
  • 谢谢,但正如我提到的 1000 个 URL 的这种方法需要 9 小时 30 分钟。所以我需要使用线程,最后如何追加,以便以上述形式获得所需的输出。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 2015-05-08
  • 2014-10-01
相关资源
最近更新 更多