我想在银行网站上搜索,但谷歌浏览器驱动程序不起作用。 如果类型为"NoneType",我想打印为"null" 我在jupyter notebook(anaconda3(中编写了这段代码。
我试图使用"如果...否则...」
from bs4 import BeautifulSoup
from urllib.request import urlopen
from selenium import webdriver
driver = webdriver.Chrome('./chromedriver.exe') html_dr = driver.execute_script('return document.body.innerHTML') #question
url_base = 'https://www.kebhana.com/cont/mall/mall08/mall0805/index.jsp?_menuNo=62608'
html = urlopen(url_base) soup = BeautifulSoup(html, "html.parser")
soup
print(soup.find_all('li', 'item type1'))
len(soup.find_all('li', 'item type1'))
from urllib.request import urljoin
name = [] url_add = [] period = [] strong = [] term = []
list_soup = soup.find_all('li', 'item type1')
for item in list_soup: name.append(item.find('a').get_text()) url_add.append(urljoin(url_base, item.find('a')['href'])) #question # # #'NoneType' object has no attribute 'get_text' #if item is not None: #period.append(item.find(class_='period').get_text()) #strong.append(item.find('strong')) #term.append(item.find(class_='term')) #else: #print("null")
url_add[:5]
name[:5]
period[:23]
strong[:23]
import pandas as pd
data = {'Name':name, 'URL':url_add} df = pd.DataFrame(data) df.head() df.to_csv('./hana_list.csv', sep=',', encoding='EUC-KR')
df['URL'][0]
html = urlopen(df['URL'][0]) soup_tmp = BeautifulSoup(html, "html.parser") soup_tmp
print(soup_tmp.find('dl', 'prodcutInfo'))
print(soup_tmp.find('div', 'exist_table'))
print(soup_tmp.find(class_='tbl_tbldiv'))
">NoneType"对象没有属性"get_text">
你叫了两次'get_text'。您已经对其中一个进行了错误检查,但没有对另一个进行错误检查。
for item in list_soup:
name.append(item.find('a').get_text()) #<--- you may also need to check item for not None here
url_add.append(urljoin(url_base, item.find('a')['href']))
if item is not None:
period.append(item.find(class_='period').get_text()) #<--- you are checking item for not None here
strong.append(item.find('strong'))
term.append(item.find(class_='term'))
else:
print("null")
或者,也许您检查错了东西。您可能需要检查item.find(class_='period') is not None
而不仅仅是if item is not None
。