Twitter API 얻었는데 파이썬으로 트위터 웹 크롤링할때 특정 위도, 경도 설정할 방법 있을까요??

조회수 1044회
  try:
    import GetOldTweets3 as got
 except:
    !pip install GetOldTweets3
    import GetOldTweets3 as got

 import requests
 from bs4 import BeautifulSoup

def get_bs_obj(url):
    result = requests.get(url)
    bs_obj = BeautifulSoup(result.content, "html.parser")

    return bs_obj

--------------------------

import datetime

days_range = []

start = datetime.datetime.strptime("2020-04-15", "%Y-%m-%d")
end = datetime.datetime.strptime("2020-04-21", "%Y-%m-%d")
date_generated = [start + datetime.timedelta(days=x) for x in range(0, (end-start).days)]

for date in date_generated:
    days_range.append(date.strftime("%Y-%m-%d"))

print("=== 설정된 트윗 수집 기간은 {} 에서 {} 까지 입니다 ===".format(days_range[0], days_range[-1]))
print("=== 총 {}일 간의 데이터 수집 ===".format(len(days_range)))

------------------------------------
import time

# 수집 기간 맞추기
start_date = days_range[0]
end_date = (datetime.datetime.strptime(days_range[-1], "%Y-%m-%d") 
            + datetime.timedelta(days=1)).strftime("%Y-%m-%d") # setUntil이 끝을 포함하지 않으므로, day + 1

# 트윗 수집 기준 정의
tweetCriteria = got.manager.TweetCriteria().setQuerySearch('Deglobalization OR  DeSinicization')\
                                           .setSince(start_date)\
                                           .setUntil(end_date)\
                                           .setMaxTweets(-1)

# 수집 with GetOldTweet3
print("Collecting data start.. from {} to {}".format(days_range[0], days_range[-1]))
start_time = time.time()

tweet = got.manager.TweetManager.getTweets(tweetCriteria)

print("Collecting data end.. {0:0.2f} Minutes".format((time.time() - start_time)/60))
print("=== Total num of tweets is {} ===".format(len(tweet)))


------------------

from random import uniform
from tqdm import tqdm_notebook

# initialize
tweet_list = []

for index in tqdm_notebook(tweet):

    # 메타데이터 목록 
    username = index.username
    link = index.permalink 
    content = index.text
    tweet_date = index.date.strftime("%Y-%m-%d")
    tweet_time = index.date.strftime("%H:%M:%S")
    retweets = index.retweets
    favorites = index.favorites

 # === 유저 정보 수집 시작 ===
    try:
        personal_link = 'https://twitter.com/' + username
        bs_obj = get_bs_obj(personal_link)
        uls = bs_obj.find("ul", {"class": "ProfileNav-list"}).find_all("li")
        div = bs_obj.find("div", {"class": "ProfileHeaderCard-joinDate"}).find_all("span")[1]["title"]

      # 가입일, 전체 트윗 수, 팔로잉 수, 팔로워 수
        joined_date = div.split('-')[1].strip()
        num_tweets = uls[0].find("span", {"class": "ProfileNav-value"}).text.strip()
        num_following = uls[1].find("span", {"class": "ProfileNav-value"}).text.strip()
        num_follower = uls[2].find("span", {"class": "ProfileNav-value"}).text.strip()

    except AttributeError:
        print("=== Attribute error occurs at {} ===".format(link))
        print("link : {}".format(personal_link))   
        pass

    # 결과 합치기
    info_list = [tweet_date, tweet_time, username, content, link, retweets, favorites, 
                 joined_date, num_tweets, num_following, num_follower]
    tweet_list.append(info_list)

    # 휴식 
    time.sleep(uniform(1,2))

----------------------


import pandas as pd

twitter_df = pd.DataFrame(tweet_list, 
                          columns = ["date", "time", "user_name", "text", "link", "retweet_counts", "favorite_counts",
                                    "user_created", "user_tweets", "user_followings", "user_followers"])

# csv 파일 만들기
twitter_df.to_csv("sample_twitter_data_{}_to_{}.csv".format(days_range[0], days_range[-1]), index=False)
print("=== {} tweets are successfully saved ===".format(len(tweet_list)))

------------------------ 

df_tweet = pd.read_csv('sample_twitter_data_{}_to_{}.csv'.format(days_range[0], days_range[-1]))
df_tweet.head(10) # 위에서 10개만 출력

-------------------

def get_keywords(dataframe):
    keywords = []
    text = dataframe["text"].lower()
    if "Deglobalization" in text:
        keywords.append("Deglobalization")
    if "desinicization" in text:
        keywords.append("desinicization")
    return ",".join(keywords)

df_tweet["keyword"] = df_tweet.apply(get_keywords,axis=1)

# barplot 그리기

import matplotlib.pyplot as plt

counts = df_tweet["keyword"].value_counts()
plt.bar(range(len(counts)), counts)
plt.title("Tweets mentioning keywords")
plt.ylabel("# of tweets")
plt.show()
print(counts)

------------------------------

# 날짜별 빈도 분석하기

counts = df_tweet["date"].value_counts().sort_index()

plt.title("Tweets mentioning keywords in time series")
plt.ylabel("# of tweets")
counts.plot(kind = 'bar')
print(counts)

출처 : https://jeongwookie.github.io/2019/06/10/190610-twitter-data-crawling/

Twiter location date
독학중이라 어떻게 어디에 코드를 넣어야지 위도와 경도를 설정하여 크롤링을 할 수 있는 방법을 몰라서 어떻게 해야만 위도와 경도를 설정해서 트위터 웹 크롤링을 할 수 있을까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자
  • 이유(목적)가 뭔가요? 엽토군 2020.5.14 15:56
  • '웹 크롤링을 통한 유럽시장 분석'을 주제로 고등학교 소논문 작성중입니다!! 알 수 없는 사용자 2020.5.14 22:10
  • 그 논문 작성하는데 '위도와 경도를 설정하는 작업'이 무슨 관계가 있나요? (그걸 물어보려고 했습니다.) 엽토군 2020.5.15 09:06
  • 스페인어를 이용해 검색하고 싶은데 미국 등 다른 나라들도 워낙 많이 쓰는 언어다 유럽지역에만 한정시켜서 크롤링 하고 싶어서요! 알 수 없는 사용자 2020.5.16 12:33

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)