2015-09-06 37 views
0

我想通过爬行浏览URL列表中是否存在特定的链接。我写了下面的程序,它完美的工作。但是,我被困在2个地方。使用Python从URL列表中查找特定的URL

  1. 而不是使用数组,我怎样才能从文本文件调用链接。
  2. 爬行器需要4分钟的时间来抓取100个网页。

有没有一种方法可以让我更快。

from bs4 import BeautifulSoup, SoupStrainer 
import urllib2 
import re 
import threading 

start = time.time() 
#Links I want to find 
url = "example.com/one", "example.com/two", "example.com/three"] 

#Links I want to find the above links in... 
url_list =["example.com/1000", "example.com/1001", "example.com/1002", 
"example.com/1003", "example.com/1004"] 

print_lock = threading.Lock() 
#with open("links.txt") as f: 
# url_list1 = [url.strip() for url in f.readlines()] 

def fetch_url(url): 
    for line1 in url_list: 
     print "Crawled" " " + line1 
     try: 
      html_page = urllib2.urlopen(line1) 
      soup = BeautifulSoup(html_page) 
      link = soup.findAll(href=True) 
     except urllib2.HTTPError: 
     pass 
     for link1 in link: 
      url1 = link1.get("href") 
      for url_input in url: 
       if url_input in url1: 
        with print_lock: 
         print 'Found' " " +url_input+ " " 'in'+ " " + line1 

threads = [threading.Thread(target=fetch_url, args=(url,)) for url in url_list] 
for thread in threads: 
thread.start() 
for thread in threads: 
thread.join() 
print('Entire job took:',time.time() - start) 
+0

由于科里的例子。总是与之斗争。 –

+0

。我已经根据几个例子进行了编辑。程序速度要快得多,但是输出会多次打印相同的答案,有时输出错误。我使用lock()函数来防止它...不工作。我还没有想出多线程。这里的任何帮助都非常感谢。提前致谢。 –

回答

0

如果您想从文本文件读取,请使用您注释掉的代码。

至于“性能”问题:您的代码会在读取操作urlopen处阻止,直到返回网站的内容为止。理想情况下,您希望并行运行这些请求。您需要一个并行解决方案,例如使用线程。

Here's使用不同的方法,使用GEVENT(非标准)

+3

你是什么意思Python自然不支持[multithreading](https://docs.python.org/2/library/multiprocessing.html)? – MattDMo

+0

多线程!=多处理。你不想让100个流程去做100个请求 – Felk

+2

你能解释一下自己吗? Python也支持[threading](https://docs.python.org/2/library/threading.html),如果你想同时运行多个I/O绑定任务,“*仍然是一个合适的模型。*” – MattDMo

相关问题