获取 API 的内容 HTML

Get the content HTML of an API

我想在 python 中获取 API 结果的内容。
这是一个 HTML 模板,里面有一张图片。

我尝试了很多方法来获取内容,但每次都不起作用。

函数 :

def screenshotlayer(self, access_key, secret_keyword, domain, args):
    domain = "http://" + domain
    url = "http://api.screenshotlayer.com/api/capture?access_key=API_KEY&url=http://google.com&viewport=1440x900&width=250"
    html = urllib2.urlopen(url).read()
    print html
    soup = BeautifulSoup(html, 'html.parser')
    return soup.findAll('img')[0]['src']

print html的时候,有很多看不懂的字
有人可以帮我解决这个问题吗?

非常感谢。

api不是returnshtml,而是原始图像文件(默认为png)。

您需要查看 status_code 是否为 200,如果是,只需将结果保存到文件中即可。

import requests

res = requests.get(
    'http://api.screenshotlayer.com/api/capture',
    params={
        'access_key': 'API_KEY',
        'url': 'http://google.com&viewport=1440x900&width=250'
    }
)
if(res.status_code == 200)
    with open('output.png', 'w+b') as f:
        f.write(res.content.encode('utf8'))
else:
    print('Api returns error: %s' % res.content)