【问题标题】:pulling data from a 'created' url从“创建”的 url 中提取数据
【发布时间】:2014-02-07 23:27:08
【问题描述】:

我有以下定义的函数,并试图弄清楚如何从 csv 中提取值并将它们放入 URL 的“theid”部分。

def get_csv_column(csv_fname, col, **kwargs):
  with open(csv_fname, 'rb') as inf:
      incsv = csv.reader(inf, **kwargs)
      column = [row[col] for row in incsv]
  return column

def withid (theid):
    """"""

    global cache

    dupe = False

    theurl = "{0}{1}{2}".format(OMDBURL, "?i=", theid)

    response = urllib2.urlopen(theurl)

    movdata = json.load(response)

    for mov in cache:
        if movdata[MKEY[1]] == mov[MKEY[1]]:
            dupe = True
    if not dupe:
        cache.append(movdata)
    return movdata
###I thought this loop below could reach in, pull the data and add a 5 second delay after each request

 with open('step3_desired_output.txt','w') as step3:
    for column in withid:
       step3.write(movdata)
       time.sleep(5)

这似乎不起作用,我不知道如何继续。

【问题讨论】:

    标签: python html json csv time


    【解决方案1】:

    Trying to run a defined function with a delay继续

    import csv
    import json
    import time
    import urllib2
    
    PAGE_DELAY = 5.    # time between loading pages
    PAGE_LOAD  = 0.3   # how long it takes to load a page
    INFILE     = 'outputrows2.csv'
    OUTFILE    = 'step3_desired_output.txt'
    
    make_url = 'http://www.imdb.com/title/tt{}/'.format
    
    def get_csv_column(csv_fname, col, **kwargs):
        with open(csv_fname, 'rb') as inf:
            incsv = csv.reader(inf, **kwargs)
            column = [row[col] for row in incsv]
        return column
    
    def get_data_by_id(id):
        url = make_url(id)
        return urllib2.urlopen(url).read()
    
    def delayed(delay, fn, *args):
        time.sleep(delay)
        return fn(*args)
    
    def human_time(seconds):
        if seconds >= 86400:
            return '{:0.1f} days'.format(seconds / 86400.)
        elif seconds >= 3600:
            return '{:0.1f} hours'.format(seconds / 3600.)
        elif seconds >= 60:
            return '{:0.1f} minutes'.format(minutes / 60.)
        else:
            return '{:0.1f} seconds'.format(seconds)
    
    def main():
        ids = get_csv_column(INFILE, 0)
    
        expected = (PAGE_DELAY + PAGE_LOAD) * len(ids)
        print('This will take about {}.'.format(human_time(expected)))
    
        results = (delayed(PAGE_DELAY, get_data_by_id, id) for id in ids)
        with open(OUTFILE, 'w') as outf:
            for res in results:
                outf.write(res)
    
    if __name__=="__main__":
        main()
    

    【讨论】:

    • 啊,我以为你已经注销了...非常感谢您的帮助...这正是我这一步所需要的。
    • @kegewe:我做到了;我回来看到你又留下了四个 cmets 和一个几乎相同的问题。
    • 对不起,我是新人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多