はじめに
「あえて」というほどでもないのですが、Pythonでフォームの編集を調べているとseleniumが良く出てくるのでRequests+BeautifulSoupで実装してみました。
以前投稿した記事の発展編としてちょうどいいのでQiitaのプロフィール文を編集してみました。
編集用関数
編集可能な多くの項目はtypeが['text', 'url', 'checkbox']のいずれかであることがソースから判明したため、ほとんど手打ちする必要なく取得できました。
取得もれである'user[desctiption]]'を追加するついでに編集すればpost用データの完成です。なぜかemail公開のチェックボックスがデフォルトでチェック済みになっていたため0に修正しています。データの作成ができたらログイン時のセッションを使ってPostすれば完成です。
def edit_profile(session):
profile_page_data = get_page_data(session, profile_url)
bs_profile_page = BeautifulSoup(profile_page_data.text, 'html.parser')
authenticity_token = str(bs_profile_page.find(attrs={'name':'authenticity_token'}).get('value'))
post_data = {
'utf-8':'✓',
'_method': 'put',
'authenticity_token':authenticity_token
}
response = bs_profile_page.find_all(attrs={'type':['text', 'url', 'checkbox']})
for i in response:
value = i.get('value')
post_data[i.get('name')] = value
post_data['user[public_email]'] = 0 # なぜかデフォルトで1
post_data['user[description]'] = '編集したよ!!!!'
print(post_data)
response = session.post(profile_url, post_data)
print(response)
いざ実行
前回作成したプログラムに上で記載したedit_profile()を追加すればプログラムの完成です。
import requests
import os
from bs4 import BeautifulSoup
import json
user_name = 'user_name'
user_password = 'user_password'
login_url = 'https://qiita.com/login'
profile_url = 'https://qiita.com/settings/profile'
def get_authenticity_token(session, login_url):
page_data = get_page_data(session, login_url)
bs_page_data = BeautifulSoup(page_data.text, 'html.parser')
authenticity_token = str(bs_login_page.find(attrs={'name':'authenticity_token'}).get('value'))
return bs_page_data, authenticity_token
def get_page_data(session, url):
response = session.get(url)
response.encoding = response.apparent_encoding
return response
def login(session):
login_form = {
'utf-8':'✓',
'authenticity_token':'token',
'identity':user_name,
'password':user_password
}
bs_login_page, authenticity_token = get_authenticity_token(session, login_url)
login_form['authenticity_token'] = authenticity_token
session.post(login_url, login_form)
def edit_profile(session):
bs_profile_page, authenticity_token = get_authenticity_token(session, profile_url)
post_data = {
'utf-8':'✓',
'_method': 'put',
'authenticity_token':authenticity_token
}
response = bs_profile_page.find_all(attrs={'type':['text', 'url', 'checkbox', 'textarea']})
for i in response:
value = i.get('value')
post_data[i.get('name')] = value
post_data['user[public_email]'] = 0 # なぜかデフォルトで1
post_data['user[description]'] = '編集したよ!!!!'
print(post_data)
response = session.post(profile_url, post_data)
print(response)
if __name__ == '__main__':
session = requests.Session()
login(session)
edit_profile(session)
実行してResponse [200]が返ってくれば成功です。