weiboからデータ取得

Python

selenium.common.exceptions.ElementNotInteractableException: Message: element

【原因】

探したい要素(今回はloginname)が複数ある場合、

find_elementすると上記エラーが起きる。

【解決方法】

loginname = find_elements()

loginname[0].send_keys

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

import chromedriver_binary

import time

import urllib

import os

import sys

import requests

path = ‘C:/Users/wiki1/Desktop/weibo/’

url2 = ‘https://weibo.com/p/1005052813841870/home?from=page_100505&mod=TAB&is_all=1#place’

name = ‘shuaiyingying’

title = ‘帅嘤嘤’

wp_user = ‘@bijo_blog’

wp_pass = ‘hIa2 ebqx dxTh ULNn djcC ioHM’

#ログイン処理(PC)

#SELENIUMで開くとスマホ画面になる

#パソコン画面と異なるので、要素が見つからず、次のエラーが起きた

#selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

#そこでウィンドウサイズを1200*1000pxに変更した

#ちなみに要素が2つ以上あっても上記エラーが発生するらしい

#詳しくは—→https://qiita.com/Sho1981/items/fc07d83b33d11a54bfcd

driver = webdriver.Chrome()

driver.get(‘https://weibo.com/login.php’)

time.sleep(12)

driver.set_window_size(1200,1000)

time.sleep(3)

driver.find_element(By.XPATH,’//*[@id=”loginname”]’).send_keys(‘00819086743966’)

driver.find_element(By.XPATH,’//*[@id=”pl_login_form”]/div/div[3]/div[2]/div/input’).send_keys(‘password8**’)

#デスクトップに空のフォルダを作成

time.sleep(2)

if not os.path.exists(path):

    os.makedirs(path)

#目的のページに移動する

#screenshot_as_pngすると次のエラーが発生した

#selenium.common.exceptions.WebDriverException: Message: unknown error: unhandled inspector error: {“code”:-32000,”message”:”Cannot take screenshot with 0 width.”}

#解決方法 —> https://www.appsloveworld.com/bestanswer/selenium/100/selenium-common-exceptions-webdriverexception-message-unknown-error-unhandled

#SELENIUMで画像を保存 –> https://junpage.com/scraping-save-image/#index_id4

imglist = []

driver.get(url2)

time.sleep(4)

#PRESS相册

#ページによって最後のdiv[5]がdiv[4]になっている

#driver.find_element(By.LINK_TEXT,” 相册 “).click()だとNoSuchElementだった

if len(driver.find_elements(By.XPATH,’//*[@id=”app”]/div[2]/div[2]/div[2]/main/div/div/div[2]/div[1]/div[2]/div/div/div[1]/div[5]/div/div’)) > 0:

    driver.find_element(By.XPATH,’//*[@id=”app”]/div[2]/div[2]/div[2]/main/div/div/div[2]/div[1]/div[2]/div/div/div[1]/div[5]/div/div’).click()

else:

    driver.find_element(By.XPATH,’//*[@id=”app”]/div[2]/div[2]/div[2]/main/div/div/div[2]/div[1]/div[2]/div/div/div[1]/div[4]/div/div’).click()

time.sleep(5)

#一旦ページ下までスクロール

#driver.execute_script(‘window.scrollTo(0, document.body.scrollHeight);’)

#が効かないページもあるのでKeyDownを繰り返してスクロールする

for i in range(10):

    driver.find_element(By.TAG_NAME,’body’).send_keys(Keys.PAGE_DOWN)

    time.sleep(1)

time.sleep(3)

#PRESS请登录后查看更多精彩内容

#ログインしてない状態でこのボタンを押すとQRコードが出てきてログインしてくださいみたいになる

driver.find_element(By.XPATH,’//*[@id=”app”]/div[2]/div[2]/div[2]/main/div/div/div[2]/div[3]/div[3]/div[2]/div[2]’).click()

#画像を取得

content = ”

imgs = driver.find_elements(By.TAG_NAME, ‘img’)

for index, img in enumerate(imgs):

    if not img.get_attribute(‘src’) == ”:

        src = img.get_attribute(‘src’)

        with urllib.request.urlopen(src) as rf:

            img_data = rf.read()

        with open(f’C:/Users/wiki1/Desktop/weibo/’ + name + f'{index}.jpg’, ‘wb’) as wf:

            wf.write(img_data)

        content += ‘<img src=”‘ + ‘https://bijo.blog/wp-content/uploads/2023/07/’ + name + str(index) + ‘.jpg”>’

#    with open(‘C:/Users/wiki1/Desktop/{index}.png’, ‘wb’) as f:

#        f.write(img.screenshot_as_png)

#wordpressのテキスト作成

data = {

    ‘title’: title,

    ‘content’: content,

    ‘categories’: 1,

    ‘tags’: 1,

    ‘slug’: title,

    ‘status’: ‘draft’,

}

res = requests.post(

    f’https://bijo.blog/wp-json/wp/v2/posts’,

    json=data,

    headers={‘Content-type’: ‘application/json’},

    auth=(wp_user,wp_pass),

)

BACK