iT邦幫忙

0

TypeError: argument of type 'NoneType' is not iterable

  • 分享至 

  • xImage
from importlib.resources import path
import profile
from click import option
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from urllib import parse
import json
import os
import pprint
import time

from sqlalchemy import false

options = webdriver.ChromeOptions()
options.add_argument('--start-maximized')
options.add_argument('--incognito')
options.add_argument('-disable-popup-blocking')
options.add_experimental_option('excludeSwitches', ['ebable-automation'])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('prefs', {"profile.password_manager_enabled":
                                          False, 'credentials_enable_service': False})
driver = webdriver.Chrome(options=options)


listData = []
url = "https://www.bookwormzz.com/zh/"


def visit():
    driver.get(url)


def getMainLinks():
    a_elms = driver.find_elements(By.CSS_SELECTOR, 'a[data-ajax="false"]')
    for a in a_elms:
        listData.append({
            "title": a.get_attribute('innerText'),
            "link": parse.unquote(a.get_attribute('herf'))+"#book_toc"
        })
    pprint.pprint(listData)


if __name__ == "__main__":
    visit()
    getMainLinks()

執行後出現

(base) PS C:\Users\Wei\Desktop\爬蟲學習> & C:/Users/Wei/anaconda3/python.exe c:/Users/Wei/Desktop/爬蟲學習/albert.py

DevTools listening on ws://127.0.0.1:52843/devtools/browser/08c6223f-6c10-415d-976e-46f16b3c1702
Traceback (most recent call last):
  File "c:\Users\Wei\Desktop\爬蟲學習\albert.py", line 48, in <module>
    getMainLinks()
  File "c:\Users\Wei\Desktop\爬蟲學習\albert.py", line 41, in getMainLinks
    "link": parse.unquote(a.get_attribute('herf'))+"#book_toc"
  File "C:\Users\Wei\anaconda3\lib\urllib\parse.py", line 656, in unquote
    if '%' not in string:
TypeError: argument of type 'NoneType' is not iterable

請問這個問題該怎麼解
上網爬文後網路上的文章告知說要加上return可是我不知道要加在哪邊

obarisk iT邦研究生 1 級 ‧ 2022-07-23 11:41:40 檢舉
要檢查 a.get_attribute('herf') 是不是空值
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答