Put text scraped in Python into Google Sheets

Reference site

https://yokonoji.work/python-scraping-6 https://qiita.com/akabei/items/0eac37cb852ad476c6b9

Library used

requests BeautifulSoup oauth2client gspread

Implementation

Service account key

The service account key for accessing Google Sheets is as written on the reference site, so write it roughly. m (_ _) m

↓ At the site https://console.developers.google.com/cloud-resource-manager

↓ execute --Create a project --Google Drive API enabled --Google Sheets API enabled --Create a service account key (JSON download)

Spreadsheet creation

--Spreadsheet creation --From "Share", share the address of "client_email" written in the downloaded JSON

Program creation

sample.py


import requests
import gspread
from bs4 import BeautifulSoup
from oauth2client.service_account import ServiceAccountCredentials

url = "<URL of the site to get>"
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
elements = soup.select('<Tags you want to get>')#select()Since it uses a method, it can be written with "CSS selector"

scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('<Downloaded JSON file name>', scope)
gc = gspread.authorize(credentials)
wks = gc.open('<Spreadsheet name>').sheet1

for index, e in enumerate(elements):
    num = index + 1 #Since there is no "0" in the spreadsheet number, add 1 first
    wks.update_acell('A'+str(num) , e.get_text())

Recommended Posts

Put text scraped in Python into Google Sheets
Clustering text in Python
Play with Google Spread Sheets in python (OAuth)
Text processing in Python
UTF8 text processing in python
Speech to speech in python [text to speech]
Inflating text data by retranslation using google translate in Python
GOTO in Python with Sublime Text 3
Put MeCab in "Windows 10; Python3.5 (64bit)"
Put python, numpy, opencv3 in ubuntu14
Until you put Python in Docker
Download Google Drive files in Python
Split iterator into chunks in python
Extract text from images in Python
Sort large text files in Python
Reading and writing text in Python
I put Python 2.7 in Sakura VPS 1GB.
[Itertools.permutations] How to put permutations in Python
PUT gzip directly to S3 in Python
Put protocol buffers into sqlite with python
Get Google Fit API data in Python
[Python] Combine multiple Excel sheets into one
Try text mining your diary in Python
Read text in images with python OCR
Put the exchange rate obtained from Oanda REST API in Python into MongoDB
First steps to try Google CloudVision in Python
Put python xgboost in max osx (llvm version)
tse --Introduction to Text Stream Editor in Python
Run Google Analytics API (core v3) in python
3 steps to put Python + mecab in yum only
To add a module to python put in Julialang
Copy data between Google Keep accounts in Python
Put Python3 in Docker container of Amazon Linux2
What to do to get google spreadsheet in python
Put out a shortened URL string in Python
Sample to put Python Kivy in one file
Snippets registered in Google Colaboratory (PDF text conversion)
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python