多线程快速抓取网页
来源:互联网 发布:ccn是什么网络 编辑:程序博客网 时间:2024/05/02 04:43
一段简单的代码,用于抓取wiki百科数据,简单的多线程编程例子,很少占内存,线程数开大了后效率很高。
import sys, thread, threading, time;import commandsfinish_num = 0;mutex = threading.Lock();def extract_qid(id, num_of_thread): try: fin = open(sys.argv[1], "r"); fout = open(sys.argv[2] + ".part" + str(id), "w"); count = 0; for line in fin: try: line = line.strip(); if count % num_of_thread != id: count += 1; continue; count += 1; _raw_query = line;cmd = "wget \"zh.wikipedia.org/zh-hans/${query}\" -O \"fetch_wiki/tmp_search_${id}\"" cmd = cmd.replace("${query}", _raw_query).replace("${id}", str(id)); commands.getoutput(cmd); tmp_fin = open("fetch_wiki/tmp_search_${id}".replace("${id}", str(id)), "r");fout.write("zh.wikipedia.org/zh-hans/${query}\n".replace("${query}",_raw_query)) for tmp_line in tmp_fin: fout.write(tmp_line) tmp_fin.close(); commands.getoutput("rm -f \"fetch_wiki/tmp_search_${id}\"".replace("${id}", str(id))); except: continue; fout.close(); fin.close(); global finish_num; if mutex.acquire(1): finish_num += 1; mutex.release(); return True; except Exception as e: print e; return False;for i in range(0, int(sys.argv[3])): thread.start_new_thread(extract_qid, (i, int(sys.argv[3])));while finish_num != int(sys.argv[3]): time.sleep(1);
- 多线程快速抓取网页
- CURL多线程抓取网页
- Python 多线程抓取网页
- c#多线程抓取网页内容
- PHP中多线程抓取网页
- python多线程抓取网页信息
- python 多线程处理抓取网页
- python多线程实现抓取网页
- PHP实现多线程抓取网页
- goLang 多线程抓取网页数据
- PHP实现多线程抓取网页
- python多线程爬虫抓取网页
- 怎样快速抓取网页中的FLASH动画
- c#多线程抓取网页内容(转)
- python beautifulsoup多线程分析抓取网页
- python beautifulsoup多线程分析抓取网页
- c#多线程抓取网页内容(转)
- PHP代码片段记录:+多线程抓取网页等
- 关于“ 以前的定义是“枚举数” ”问题的解决办法
- Navigation Drawer(导航抽屉)模式
- 数据结构线性表的基本函数
- hdu oj 2000
- 编程小白的记录之路
- 多线程快速抓取网页
- 机房收费系统的上下机
- 不是技术牛人,如何拿到国内IT巨头的Offer
- 软件项目开发模板(邮件接收器)
- Linux命令的一般通用格式
- hadoop2.0 ha failover 遇到的问题
- 【单调队列】poj 2823 Sliding Windows
- EL表达式
- 转发与重定向