如何制作页面循环纸条

2024-04-25 05:41:19 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有人可以让这个脚本下载多个页面,而不是一个单一的页面?你知道吗

这是一个Python脚本:

import json, urllib

page_data = urllib.urlopen("http://example.com/wp-content/themes/example/html5system/request/example-8/get/1/").read()
js = json.loads(page_data)
#print js['page']

with open('page1.svg', 'wb') as f_out:
    f_out.write(js['page'].encode('utf-8'))  

Tags: import脚本comjsonhttpdataexamplepage
1条回答
网友
1楼 · 发布于 2024-04-25 05:41:19
import json, urllib

for i in range(1,301): # loop from 1 to 300
    page_data = urllib.urlopen("http://example.com/wp-content/themes/example/html5system/request/example-8/get/%d/" % i).read()
    js = json.loads(page_data)

    with open('page%d.svg' % i, 'wb') as f_out: # save each page in a separate svg
        f_out.write(js['page'].encode('utf-8'))
        f_out.close()

我只是在现有代码的基础上添加了一个简单的循环,用于1到300的数字。然后我做了一个字符串替换'%d' % i,它插入了i的值,其中%d位于字符串中。我对文件名也做了同样的处理,所以每个页面都保存到一个单独的.svg文件中。你知道吗

相关问题 更多 >