<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>【python twitter トレンド】タグの記事一覧｜python-manブログ</title>
	<atom:link href="https://python-man.club/tag/python-twitter-%e3%83%88%e3%83%ac%e3%83%b3%e3%83%89/feed/" rel="self" type="application/rss+xml" />
	<link>https://python-man.club</link>
	<description>独学者でもpythonでアプリを作れる！</description>
	<lastBuildDate>Sun, 05 Jun 2022 15:18:04 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.2</generator>
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/><atom:link rel="hub" href="https://websubhub.com/hub"/>	<item>
		<title>pythonでtwitterのトレンドを取得する【サンプルコードあり】</title>
		<link>https://python-man.club/python_twitter_trends/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=python_twitter_trends</link>
					<comments>https://python-man.club/python_twitter_trends/#respond</comments>
		
		<dc:creator><![CDATA[syou0445]]></dc:creator>
		<pubDate>Tue, 19 Apr 2022 10:51:35 +0000</pubDate>
				<category><![CDATA[python]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[python twitter トレンド]]></category>
		<guid isPermaLink="false">https://python-man.club/?p=1056</guid>

					<description><![CDATA[pythonでtwitterを操作することで、さまざまな情報を取得することができます 取得できる情報はこちらの記事を参考にしてください https://python-man.club/python_twitter_api]]></description>
										<content:encoded><![CDATA[<p>pythonでtwitterを操作することで、さまざまな情報を取得することができます</p>
<p>取得できる情報はこちらの記事を参考にしてください</p>
<p>https://python-man.club/python_twitter_api_tweet/</p>
<p><strong>今回の記事では、pythonでtwitterのトレンドを取得する方法について解説していきたいと思います</strong></p>
<div class="simple-box2">
<p style="text-align: center;"><strong><span class="marker2">pythonでtwitterを操作してみたい</span></strong></p>
<p style="text-align: center;"><strong><span class="marker2">pythonでtwitterのトレンドを取得したい</span></strong></p>
<p style="text-align: center;"><strong>という方向けに、<span class="marker">pythonを使ったtwitterのトレンド取得方法について</span></strong>解説していきます</p>
<p>以下の記事では、pythonでtwitterを使う方法をまとめて解説しているので、参考にしてみてください</p>
<p><a href="https://python-man.club/python_twitter/">pythonでtwitterを使ってみよう</a></p>
</div>
<p>pythonでtwitterを扱えるようになると、twitterで行われている懸賞に自動で応募することができるようになります</p>
<p>https://python-man.club/python_twitter_auto_retweet/</p>
<h2>pythonでtwitterのトレンドを取得する</h2>
<img fetchpriority="high" decoding="async" class="alignnone size-large wp-image-1058" src="https://python-man.club/wp-content/uploads/2022/04/e436104fc9e0d339f623f74dc11e823bff4c77a0-1024x683.jpeg" alt="python twitter トレンド" width="1024" height="683" srcset="https://python-man.club/wp-content/uploads/2022/04/e436104fc9e0d339f623f74dc11e823bff4c77a0-1024x683.jpeg 1024w, https://python-man.club/wp-content/uploads/2022/04/e436104fc9e0d339f623f74dc11e823bff4c77a0-300x200.jpeg 300w, https://python-man.club/wp-content/uploads/2022/04/e436104fc9e0d339f623f74dc11e823bff4c77a0-768x512.jpeg 768w, https://python-man.club/wp-content/uploads/2022/04/e436104fc9e0d339f623f74dc11e823bff4c77a0.jpeg 1200w, https://python-man.club/wp-content/uploads/2022/04/e436104fc9e0d339f623f74dc11e823bff4c77a0-1024x683.jpeg 856w" sizes="(max-width: 1024px) 100vw, 1024px" />
<p>pythonでtwitterのトレンドを取得するには、事前準備が必要になります</p>
<p><strong><span class="marker">まずは、twitter Developersに登録し、API認証を受ける必要があります</span></strong></p>
<p>https://python-man.club/python_twitter_api/</p>
<h3>tweepyのインストール</h3>
<p>twitter APIの認証が完了したら、tweepyをインストールしておきます</p>
<p>Macの方は、ターミナル、windowsであればコマンドプロンプトで以下のコマンドを入力します</p>
<pre class="language-python"><code>pip install tweepy</code></pre>
<p>これで準備完了です</p>
<p><strong>tweepyについて詳しく知りたい方は、こちらの記事を参考にしてください</strong></p>
<p>https://python-man.club/python_twitter_tweepy/</p>
<h3>WOEID</h3>
<p><strong><span class="marker">WOEIDはWhere On Earth IDentifierを指します</span></strong></p>
<p><span style="color: #ff0000;"><strong>各国・都市にそれぞれ振られたIDであり、twitter APIではWOEIDを使用して、地域を指定・トレンドを取得していきます</strong></span></p>
<h4>WOEIDの割り振り</h4>
<table class="has-fixed-layout">
<thead>
<tr>
<th>都市</th>
<th class="has-text-align-center" data-align="center">WOEID</th>
</tr>
</thead>
<tbody>
<tr>
<td>千葉</td>
<td class="has-text-align-center" data-align="center">1117034</td>
</tr>
<tr>
<td>福岡</td>
<td class="has-text-align-center" data-align="center">1117099</td>
</tr>
<tr>
<td>浜松</td>
<td class="has-text-align-center" data-align="center">1117155</td>
</tr>
<tr>
<td>広島</td>
<td class="has-text-align-center" data-align="center">1117227</td>
</tr>
<tr>
<td>川崎</td>
<td class="has-text-align-center" data-align="center">1117502</td>
</tr>
<tr>
<td>北九州</td>
<td class="has-text-align-center" data-align="center">1110809</td>
</tr>
<tr>
<td>神戸</td>
<td class="has-text-align-center" data-align="center">1117545</td>
</tr>
<tr>
<td>熊本</td>
<td class="has-text-align-center" data-align="center">1117605</td>
</tr>
<tr>
<td>京都</td>
<td class="has-text-align-center" data-align="center">15015372</td>
</tr>
<tr>
<td>名古屋</td>
<td class="has-text-align-center" data-align="center">1117817</td>
</tr>
<tr>
<td>新潟</td>
<td class="has-text-align-center" data-align="center">1117881</td>
</tr>
<tr>
<td>岡山</td>
<td class="has-text-align-center" data-align="center">90036018</td>
</tr>
<tr>
<td>沖縄</td>
<td class="has-text-align-center" data-align="center">2345896</td>
</tr>
<tr>
<td>大阪</td>
<td class="has-text-align-center" data-align="center">15015370</td>
</tr>
<tr>
<td>相模原</td>
<td class="has-text-align-center" data-align="center">1118072</td>
</tr>
<tr>
<td>埼玉</td>
<td class="has-text-align-center" data-align="center">1116753</td>
</tr>
<tr>
<td>札幌</td>
<td class="has-text-align-center" data-align="center">1118108</td>
</tr>
<tr>
<td>仙台</td>
<td class="has-text-align-center" data-align="center">1118129</td>
</tr>
<tr>
<td>高松</td>
<td class="has-text-align-center" data-align="center">1118285</td>
</tr>
<tr>
<td>東京</td>
<td class="has-text-align-center" data-align="center">1118370</td>
</tr>
<tr>
<td>横浜</td>
<td class="has-text-align-center" data-align="center">1118550</td>
</tr>
<tr>
<td>日本</td>
<td class="has-text-align-center" data-align="center">23424856</td>
</tr>
</tbody>
</table>
<p>日本国内のWOEIDは上記です</p>
<p>国内のトレンドを取得したい場合には、上記いずれかを指定すればOKです</p>
<p>各国のWOEIDを知りたい場合には、以下のサイトで調べることができます</p>
<p><a href="https://www.findmecity.com">findmecity</a></p>
<h2>pythonでtwitterのトレンドを取得するコード</h2>
<img decoding="async" class="alignnone size-large wp-image-1029" src="https://python-man.club/wp-content/uploads/2022/04/merakist-CNbRsQj8mHQ-unsplash-1024x768.jpg" alt="コード解説" width="1024" height="768" srcset="https://python-man.club/wp-content/uploads/2022/04/merakist-CNbRsQj8mHQ-unsplash-1024x768.jpg 1024w, https://python-man.club/wp-content/uploads/2022/04/merakist-CNbRsQj8mHQ-unsplash-300x225.jpg 300w, https://python-man.club/wp-content/uploads/2022/04/merakist-CNbRsQj8mHQ-unsplash-768x576.jpg 768w, https://python-man.club/wp-content/uploads/2022/04/merakist-CNbRsQj8mHQ-unsplash-1536x1152.jpg 1536w, https://python-man.club/wp-content/uploads/2022/04/merakist-CNbRsQj8mHQ-unsplash.jpg 1920w, https://python-man.club/wp-content/uploads/2022/04/merakist-CNbRsQj8mHQ-unsplash-1024x768.jpg 856w" sizes="(max-width: 1024px) 100vw, 1024px" />
<p>まずは全体のコードから</p>
<p>今回は取得したデータをpandasのデータフレームに変換しています</p>
<pre class="language-python"><code>import tweepy
import pandas as pd

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_SECRET = ''

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)

#千葉のWOEID
woeid = 1117227
#トレンド一覧取得
trends = api.get_place_trends(woeid)
df = pd.DataFrame(trends[0]["trends"])
print(df)</code></pre>
<h3>twitter APIの認証</h3>
<p>まずはtwitter APIの認証です</p>
<pre class="language-python"><code>import python 

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_SECRET = ''

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)</code></pre>
<h3>トレンドの取得</h3>
<p>tweepyを使ってトレンドを取得するには、API.get_place_trends()を使用します</p>
<pre class="language-python"><code>#千葉のWOEID
woeid = 1117227
#トレンド一覧取得
trends = api.get_place_trends(woeid)</code></pre>
<h3>取得したトレンドをデータフレームにする</h3>
<p>トレンドを取得したら、pandasのデータフレームにしていきます</p>
<pre class="language-python"><code>df = pd.DataFrame(trends[0]["trends"])</code></pre>
<img decoding="async" class="alignnone size-large wp-image-1057" src="https://python-man.club/wp-content/uploads/2022/04/スクリーンショット-2022-04-19-19.42.12-1024x496.jpg" alt="tweepy トレンド" width="1024" height="496" srcset="https://python-man.club/wp-content/uploads/2022/04/スクリーンショット-2022-04-19-19.42.12-1024x496.jpg 1024w, https://python-man.club/wp-content/uploads/2022/04/スクリーンショット-2022-04-19-19.42.12-300x145.jpg 300w, https://python-man.club/wp-content/uploads/2022/04/スクリーンショット-2022-04-19-19.42.12-768x372.jpg 768w, https://python-man.club/wp-content/uploads/2022/04/スクリーンショット-2022-04-19-19.42.12-1536x744.jpg 1536w, https://python-man.club/wp-content/uploads/2022/04/スクリーンショット-2022-04-19-19.42.12.jpg 1900w, https://python-man.club/wp-content/uploads/2022/04/スクリーンショット-2022-04-19-19.42.12-1024x496.jpg 856w" sizes="(max-width: 1024px) 100vw, 1024px" />
<h2>まとめ</h2>
<ul>
<li><strong>トレンド取得にはAPI.get_place_trends()を使用</strong></li>
<li><strong>各地域でトレンドは異なるのでWOEIDで指定する</strong></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://python-man.club/python_twitter_trends/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
