카테고리 없음

[파이썬] Rest API 및 Tweepy를 사용하여 트윗에서 전체 JSON 데이터 다운로드, 트윗 ID로 쿼리

행복을전해요 2021. 2. 18. 07:36

그것을 알아 냈습니다. 이 끔찍한 실수를 한 사람을 위해 (시작할 모든 데이터를 가져 오십시오!) 여기에 ID 번호를 추출하여 목록으로 저장하는 정규식이 포함 된 코드가 있습니다.

import re

# Read in your ugly text file.
tweet_string = open('nameoffile.txt', 'rU')
tweet_string = tweet_string.read()

# Find all the id numbers with a regex.
id_finder = re.compile('[0-9]{18,18}')

# Go through the twee_string object and find all 
# the IDs that meet the regex criteria.
idList = re.findall(id_finder, tweet_string)

이제 목록을 반복하고 idList각 ID를 api에 대한 쿼리로 제공 할 수 있습니다 (인증을 완료하고 api 클래스의 인스턴스가 있다고 가정). 그런 다음 목록에 추가 할 수 있습니다. 다음과 같은 것 :

tweet_list = []
for id in idList:
    tweet = api.get_status(id)
        tweet_list.append(tweet)
        

중요한 참고 사항 : tweet_list변수에 추가되는 것은 tweepy status object. 이에 대한 해결 방법이 필요하지만 위의 문제가 해결되었습니다.



출처
https://stackoverflow.com/questions/22079850