2016-08-08 3 views
0

나는 파이썬에서 API 결과의 내용을 얻으려고하고있다.
이미지가있는 HTML 템플릿입니다.API의 내용 HTML 얻기

콘텐츠를 얻기 위해 많은 방법을 시도했지만 매번 작동하지 않습니다.

fuction를 :

I print 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'] 

.
누군가이 문제를 호소하도록 도와 줄 수 있습니까?

정말 고마워요.

+1

api는 HTML이 아닌 원시 이미지 파일 (기본값은 png)을 반환합니다. – Cyrbil

답변

3

api는 HTML을 반환하지 않고 원시 이미지 파일 (기본값은 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)