Crawling with Python and Twitter API 1-Simple search function

Motivation

In order to learn about crawling and how to handle APIs, I decided to actually make a crawling tool for Twitter.

environment

The environment for this development is as follows.

MacBook Air (Retina, 13-inch, 2018) Processor 1.6 GHz Intel Core i5 Memory 8 GB 2133 MHz LPDDR3

API installation procedure

To install the API, follow the steps below.

  1. Get Twitter API key
  2. Crawler mounting

1. Get Twitter API key

First, get your Twitter API key on this site  Twitter Developers After logging in with the account you want the API key here, click Create an app and then press Approve. After that, you will be asked variously, but I will answer them appropriately. Then you will get the following four API keys.

Consumer Key  Consumer Secret Key  Access Token  Access Secret Token

2. Crawler mounting

We will implement the crawler so that it can also be used as a module First, create the following config.py

config.py


Consumer_Key = "Comsumer Key"
Consumer_Secret_Key = "Consumer Secret Key"
Access_Token = "Access Token"
Access_Secret_Token = "Access Secret Token"

Next, create an empty file called __init__.py

__init__.py



Then we will use these files to implement the crawler. The name of the crawler file to be implemented is tweet_butler.py. Create this in the same hierarchy as the config.py file and implement it.

import

tweet_butler.py


from . import config
from requests_oauthlib import OAuth1Session
import json

Please note that the from .import config part is imported with a relative path, and if there is no __init __.py file, an Import error will occur. The config.py file is needed to use the API key, and the requests_oauthlib library is needed for OAuth authentication. Furthermore, since the API response is returned as a json file, import the json library as well. Next, search using the API.

tweet_butler.py


CK = config.Consumer_Key
CS = config.Consumer_Secret_Key
AT = config.Access_Token
AS = config.Access_Secret_Token

twitter = OAuth1Session(CK, CS, AT, AS)
params = { q : "Search word" }
res = twitter.get(url, params = params)

Now you get the response from the API. Then make sure the status code is 200 and then get the text in json format.

if res.status_code == 200:
    search_result = json.loads(res.text)

We want to perform these operations as a class, so we will prepare a new Butler class.

Butler class

tweet_butler.py


class Butler:
    def __init__(self,setting = config):
        self.api = OAuth1Session(setting.Consumer_Key, setting.Consumer_Secret_Key, setting.Access_Token, setting.Access_Secret_Token)
        self.search_url = "https://api.twitter.com/1.1/search/tweets.json"

    def search(self,word,count=10):
        params = {"q":word,"count":count}
        res = self.api.get(self.search_url, params = params)
        if res.status_code == 200:
            search_results = json.loads(res.text)
        tweets = [Tweet(result) for result in search_results["statuses"]]
        return tweets

When receiving the search results, there is a code called tweet (result), but since it is troublesome to handle tweets in dictionary format, we create a separate tweet class and pass a dictionary type array there.

Tweet class

tweet_butler.py


class Tweet:
    def __init__(self,tweet):
        self.dict = tweet
        self.tweet_id = tweet["id"]
        self.user_id = tweet["user"]["id"]
        self.user_name = tweet["user"]["screen_name"]
        self.text = tweet["text"]
        self.created_at = tweet["created_at"]
        self.favorite_count = tweet["favorite_count"]
        self.hashtags = tweet["entities"]["hashtags"]
        self.symbols = tweet["entities"]["symbols"]
        self.mention = tweet["entities"]["user_mentions"]
    
    def get_text(self):
        return self.text

At this point, we only need the text from the tweet, so we're making the tweet class easy. Now you can get the text by doing get_text on the list of tweets received from butler.

Continue

Next, I would like to add a function to search user's profile to Butler and create a class for user.

reference

[Crawler creation using Twitter API](https://datumstudio.jp/blog/twitterapi%E3%82%92%E7%94%A8%E3%81%84%E3%81%9F%E3%82%AF % E3% 83% AD% E3% 83% BC% E3% 83% A9% E3% 83% BC% E4% BD% 9C% E6% 88% 90) twitter developer

Recommended Posts

Crawling with Python and Twitter API 1-Simple search function
Crawling with Python and Twitter API 2-Implementation of user search function
Search twitter tweets with python
Try hitting the Twitter API quickly and easily with Python
I tried follow management with Twitter API and Python (easy)
Collecting information from Twitter with Python (Twitter API)
Simple Slack API client made with Python
Automatically search and download YouTube videos with Python
Causal reasoning and causal search with Python (for beginners)
I tried function synthesis and curry with python
Get Gmail subject and body with Python and Gmail API
Sequential search with Python
Binary search with python
Binary search with Python3
Search Twitter using Python
I tried to automate internal operations with Docker, Python and Twitter API + bonus
Automate UI testing with Selenium API | Crawling websites with python
Let's make a simple game with Python 3 and iPhone
[Python] Get user information and article information with Qiita API
[Vagrant] Set up a simple API server with python
Twitter posting client made with Flask with simple login function
Perform a Twitter search from Python and try to generate sentences with Markov chains.
Programming with Python and Tkinter
Encryption and decryption with Python
Use Trello API with python
Python and hardware-Using RS232C with Python-
Twitter graphing memo with Python
Full bit search with Python
Play RocketChat with API / Python
Support yourself with Twitter API
Call the API with python3.
python with pyenv and venv
Search engine work with python
Use subsonic API with python3
[Python] Depth-first search and breadth-first search
Successful update_with_media with twitter API
Streamline web search with python
Works with Python and R
Easy to use Nifty Cloud API with botocore and python
Try Amazon Simple Workflow Service (SWF) with Python and boto3
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Simple sales tool creation with Python GUI: Employee number search
Streamline information gathering with the Twitter API and Slack bots
Web crawling, web scraping, character acquisition and image saving with python
Collecting information from Twitter with Python (MySQL and Python work together)
Aggregate and analyze product prices using Rakuten Product Search API [Python]
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
I made a simple circuit with Python (AND, OR, NOR, etc.)
Operate Jupyter with REST API to extract and save Python code
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
Rubyist tried to make a simple API with Python + bottle + MySQL
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Get reviews with python googlemap api
Run Rotrics DexArm with python API
Specifying the date with the Twitter API
Scraping with Python, Selenium and Chromedriver