Twitter API 얻었는데 파이썬으로 트위터 웹 크롤링할때 특정 위도, 경도 설정할 방법 있을까요??
조회수 1053회
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
독학중이라 어떻게 어디에 코드를 넣어야지 위도와 경도를 설정하여
크롤링을 할 수 있는 방법을 몰라서 어떻게 해야만 위도와 경도를 설정해서 트위터 웹 크롤링을 할 수 있을까요?
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력