Get Qiita trends with Python scraping

Introduction

I referred to the following article.

Introduction to Python Web Scraping Practice BeautifulSoup4 cheat sheet (selector, etc.)

The data we are aiming for this time is the trend on the top page of Qiita.

スクリーンショット 2020-01-11 0.48.34.png

in this way <div data-hyper-app ='Trend' data-hyper-props ='trend json data'> Since the trend JSON data exists in the form of, the goal is to acquire it.

Implementation

We use a library called Beautiful Soup.

python


import urllib.request
from bs4 import BeautifulSoup
import json

QIITA_TOP_URL = 'https://qiita.com/'
def get_trend_items():
    req = urllib.request.Request(QIITA_TOP_URL)
    with urllib.request.urlopen(req) as res:
        body = res.read()
        soup = BeautifulSoup(body, "html.parser")
        target_div = soup.select('div[data-hyperapp-app="Trend"]')[0]
        trend_items = json.loads(target_div.get('data-hyperapp-props'))
    return trend_items

Recommended Posts

Get Qiita trends with Python scraping
[Python] Get Qiita trends
Scraping with Python
Scraping with Python
Get weather information with Python & scraping
Get property information by scraping with python
Scraping with Python (preparation)
Try scraping with Python.
Scraping with Python + PhantomJS
Get date with python
Scraping with Selenium [Python]
Scraping with Python + PyQuery
Scraping RSS with Python
Get country code with python
I tried scraping with Python
Web scraping with python + JupyterLab
Scraping with selenium in Python
Scraping with Selenium + Python Part 1
Get Twitter timeline with python
Festive scraping with Python, scrapy
Get Youtube data with python
Scraping with Tor in Python
Get thread ID with python
Scraping weather forecast with python
Scraping with Selenium + Python Part 2
Get started with Python! ~ ② Grammar ~
I tried scraping with python
Get stock price with Python
Web scraping beginner with python
Get home directory with python
Get keyboard events with python
Get Alembic information with Python
[First API] Try to get Qiita articles with Python
[Python] Get user information and article information with Qiita API
Try scraping with Python + Beautiful Soup
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Scraping with Node, Ruby and Python
Web scraping with Python ① (Scraping prior knowledge)
Get reviews with python googlemap api
Scraping with Selenium in Python (Basic)
Web scraping with Python First step
I tried web scraping with python.
Scraping with Python and Beautiful Soup
Quine Post with Qiita API (Python)
Get web screen capture with python
Get the weather with Python requests 2
[Python] Get economic data with DataReader
How to get started with Python
Let's do image scraping with Python
"Scraping & machine learning with Python" Learning memo
Get started with Python in Blender
I tried scraping the ranking of Qiita Advent Calendar with Python
Get additional data in LDAP with python
Get html from element with Python selenium
Try logging in to qiita with Python
[Note] Get data from PostgreSQL with Python
WEB scraping with Python (for personal notes)
Automate simple tasks with Python Part1 Scraping
Getting Started with Python Web Scraping Practice
I tried scraping Yahoo News with Python