Created
May 4, 2017 15:26
-
-
Save sxslex/e2c595b5f1a3b7cd7fc68a5c44f2f81e to your computer and use it in GitHub Desktop.
Busca os dados de um JSON paginando
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def busca_noticias(url, limit=10, debug=False): | |
offset = 0 | |
while True: | |
urlget = url.format(limit=limit, offset=offset) | |
if debug: | |
print('#### GET: ' + urlget) | |
resp = requests.get(urlget).json() | |
for noticia in resp['ok']['noticias']: | |
yield noticia | |
if len(resp['ok']['noticias']) < limit: | |
return | |
offset += limit | |
for idx, noticia in enumerate(busca_noticias( | |
url=( | |
'http://app.alterosa.com.br/apps,49,5/' | |
'ultimas-noticias-import?limit={limit}' | |
'&offset={offset}&hash=743aac81bf8da79' | |
'acb5285a65abfa65d&acessadas=0&d1=02/05' | |
'/2017&d2=03/05/20' | |
), | |
limit=10, | |
debug=True | |
)): | |
print(str(idx) + ' ' + noticia['titulo']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment