forked from zhongyingqun/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.py
More file actions
71 lines (58 loc) · 1.72 KB
/
thread.py
File metadata and controls
71 lines (58 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# coding:utf-8
import urllib2
import Queue
import threading
urls = ["http://m.sohu.com/"]
crawledurl = []
crawl_queue = Queue.Queue()
def get_next_link(page):
start_link = page.find("href=")
if start_link == -1:
return None, 0
start_quote = page.find('"', start_link)
end_quote = page.find('"', start_quote + 1)
url = page[start_quote + 1:end_quote]
return url, end_quote
#¿¿¿¿
def get_all_links(page):
while True:
url, endpos = get_next_link(page)
if url == None:
break
elif url.startswith("http"):
#print "find:" + url
#urls.append(url)
crawl_queue.put(url)
page = page[endpos+1:]
else:
page = page[endpos+1:]
class CrawlUrl(threading.Thread):
"""¿¿¿¿¿¿¿html¿¿¿¿¿¿¿"""
def __init__(self, crawl_queue):
threading.Thread.__init__(self)
self.crawl_queue = crawl_queue
def run(self):
while True:
url = self.crawl_queue.get()
if url not in crawledurl:
try:
print "tocrawl:" + url
resp = urllib2.urlopen(url)
page= resp.read()
get_all_links(page)
crawledurl.append(url)
except:
pass
else:
pass
self.crawl_queue.task_done()
def main():
for i in range(30):
crawlthread = CrawlUrl(crawl_queue)
crawlthread.setDaemon(True)
crawlthread.start()
for url in urls:
crawl_queue.put(url)
crawl_queue.join()
if __name__ == "__main__":
main()