简体   繁体   English

简单脚本运行非常慢

[英]Simple script running very slow

I wrote this simple script to check whether or not a set of Bitcoin addresses have had transactions. 我写了这个简单的脚本来检查一组比特币地址是否有交易。 However I think it's running very slowly because it's processing 2 per second more or less. 但是我认为它运行非常缓慢,因为它每秒或多或少地处理2个。 The file has over 60k addresses so... this is gonna take forever! 该文件有超过6万个地址,因此...这将永远花费!

Is that ok? 这可以吗?

import urllib2

f = open('bc', 'r')
output = open('output', 'w')

n = 1
for bc in f:
    url = "https://blockchain.info/address/" + bc
    aux = urllib2.urlopen(url).read()    
    print n
    if int(aux[aux.find("<td id=\"n_transactions\">") + 24]) > 0:
        text = str(n) + ' -- ' + bc
        output.write(text)
    n = n + 1

output.close()
f.close()

You're opening a URL for every single instance in the file... it's going to take time to load the request, perform the find, and then write to file. 您正在为文件中的每个实例打开一个URL……这将花费一些时间来加载请求,执行查找并写入文件。 You're loading a webpage 60,000 times... it's going to take a while. 您正在加载一个网页60,000次...这将需要一段时间。

Not necessarily the scripts fault, it's also your connection, the site's speed, etc. Lot of variables in your ability to process the data. 脚本不一定出错,这也取决于您的连接,站点的速度等。处理数据的能力中有很多变量。

I don't see any problems with the script. 我认为该脚本没有任何问题。 Every page-load operation will have an impact on the output rendering time. 每个页面加载操作都会对输出呈现时间产生影响。

Perhaps why don't you think about some kind of library to perform this operation. 也许您为什么不考虑使用某种库来执行此操作。

See if Python's PANDAS - pandas.pydata.org can help you. 看看Python的PANDAS-pandas.pydata.org是否可以为您提供帮助。 The biggest benefit that PANDAS will bring with itself is it can be used very efficiently to merge multiple files and perform operations on it collectively. PANDAS将带来的最大好处是,可以非常有效地使用它来合并多个文件并对其集体执行操作。 This way you can reduce the page executions which would further make the script little more faster. 这样,您可以减少页面执行量,这将进一步使脚本快一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM