简体   繁体   中英

Python error - TypeError: object of type 'NoneType' has no len()

I want to read the price from the below site. But when I run, I got [TypeError: object of type 'NoneType' has no len()] error.

word = '0735850859699'
driver = webdriver.Chrome('chromedriver.exe')
url = driver.get("https://www.lotteon.com/p/product/LM" + str(word))
time.sleep(3)

soup = BeautifulSoup(url, 'lxml')
content = soup.findAll("div", {"class":"price"}).text
print(content)

enter image description here

Your url object is a Nonetype from.get(). You want to use the method.page_source:

from selenium import webdriver
from bs4 import BeautifulSoup

word = '0735850859699'
firefoxoptions = webdriver.FirefoxOptions()
firefoxoptions.set_headless()
driver = webdriver.Firefox(executable_path=r"<gecko_driver_path_here>", 
                           firefox_options=firefoxoptions)
driver.get(f"https://www.lotteon.com/p/product/LM{word}")
url = driver.page_source

soup = BeautifulSoup(url, 'lxml')
content = soup.find("div", {"class":"price"}).text
print(content)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM