C++ 或 Python : Bruteforcing to find text in many webpages

标签 c++ python html webpage

假设我想暴力破解网页:

例如,http://www.example.com/index.php?id= <1 - 99999> 并搜索每个页面以查找是否包含特定文本。 如果页面包含文本,则将其存储到字符串中

我有点让它在 python 中工作,但它很慢(每页大约 1-2 秒,这将需要大约 24 小时才能完成)是否有更好的解决方案?我正在考虑使用 C/C++,因为我听说 python 效率不高。不过转念一想,我觉得可能不是python的效率,而是访问html元素的效率(我把整个html改成文本,然后搜索一下。。。内容挺长的)

那么如何提高暴力破解的速度呢?

最佳答案

您的问题很可能与您快速解析 HTML 的能力无关,而与页面检索延迟和顺序任务阻塞有关。

1-2 秒是检索页面的合理时间。您应该能够更快地在页面上找到文本数量级。但是,如果您一次处理一个页面,那么您可能会在查找结果时等待 Web 服务器的响应而受阻。您可以改为通过工作进程一次检索多个页面,然后只等待它们的输出。

以下代码已从 Python's multiprocessing docs 修改而来更贴近您的问题。

import urllib
from multiprocessing import Process, Queue

def worker(input, output):
  for func, args in iter(input.get, 'STOP'):
    result = func(*args)
    output.put(result)

def find_on_page(num):
  uri = 'http://www.example.com/index.php?id=%d' % num
  f = urllib.urlopen(uri)
  data = f.read()
  f.close()
  index = data.find('datahere:') # obviously use your own methods
  if index < 0:
    return None
  else:
    return data[index:index+20]

def main():
  NUM_PROCESSES = 4
  tasks = [(find_on_page, (i,)) for i in range(99999)]
  task_queue = Queue()
  done_queue = Queue()
  for task in tasks:
    task_queue.put(task)
  for i in range(NUM_PROCESSES):
    Process(target=worker, args=(task_queue, done_queue)).start()
  for i in range(99999):
    print done_queue.get()
  for i in range(NUM_PROCESSES):
    task_queue.put('STOP')

if __name__ == "__main__":
  main()

关于C++ 或 Python : Bruteforcing to find text in many webpages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5724985/

相关文章:

c++ - 同样的表达方式却不同的结果

java - 如何使用 Necessitas(qt 端口)构建我的 HelloWorld Android 应用程序?

python - 比较从 float 和字符串创建的 Python Decimals

javascript - 数据百分比属性更改的 CSS 转换

html - v-slot :opposite doesn't work. vuetify 时间线项目

html - 如何在容器内居中对齐并获得统一的宽度

c++ - 整数必须存储在连续的内存地址中吗?

c++ - 在 .h 文件中声明静态常量 vector 时出错,在 .cpp 文件中定义时出错

python - 使用first_project.urls中定义的URLconf,Django尝试了这些URL模式,按顺序:

python - 跟踪 python : only include some files