- Python Social Media Analytics
- Siddhartha Chatterjee Michal Krystyanczuk
- 286字
- 2021-07-15 17:24:57
Using requests to connect
Firstly, we include all necessary libraries. We have added the json library to be able to parse easily the outputs of the Twitter API and urllib.parse, which encodes a query string into a proper request URL:
import requests from requests_oauthlib import OAuth1 import json from urllib.parse import urlparse
In the first place, we define parameters that will be used to establish connections with the Twitter API and we create an OAuth client connection:
params = { 'app_key':'YOUR_APP_KEY',
'app_secret':'YOUR_APP_SECRET',
'oauth_token':'USER_OAUTH_TOKEN',
'oauth_token_secret':'USER_OAUTH_TOKEN_SECRET' } auth = OAuth1( params['app_key'],
params['app_secret'],
params['oauth_token'],
params['oauth_token_secret'] )
Firstly, we encode our query. We have chosen to search for three car brands: BMW, Mercedes, and Audi:
q = urlparse('BMW OR Mercedes OR Audi')
Then we execute a search request using our query and OAuth client:
results = requests.get(url_rest, auth=auth)
The request returned a list of tweets with all the meta information. We will convert it to JSON and print the content of each tweet we find under the text field.
for tweet in results.json(): print (tweet['text'])
Similarly, we make a request to the Streaming API to get all recent tweets.
stream_results = requests.get(url_streaming, stream=True)
We keep iterating through all the lines that are being returned.
for line in stream_results.iter_lines(): if line: decoded_line = line.decode('utf-8') print(json.loads(decoded_line)['text'])
If the line exists we decode it to UTF-8 to make sure we manage the encoding issues and then we print a field text from JSON.
We will use both methods to get and pre-process data in a practical example in Chapter 4, Analyzing Twitter Using Sentiment Analysis and Entity Recognition.
- AngularJS Testing Cookbook
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- Microsoft Dynamics 365 Extensions Cookbook
- AngularJS Web Application Development Blueprints
- Cassandra Design Patterns(Second Edition)
- JavaScript by Example
- C語言程序設計實驗指導 (第2版)
- 微服務架構深度解析:原理、實踐與進階
- C# and .NET Core Test Driven Development
- 編寫高質量代碼:改善Objective-C程序的61個建議
- C語言程序設計簡明教程:Qt實戰
- 響應式Web設計:HTML5和CSS3實戰(第2版)
- Apache Camel Developer's Cookbook
- Swift 2 Design Patterns
- 微信公眾平臺開發最佳實踐