【问题标题】:BeautifulSoup returns None python seleniumBeautifulSoup 返回无 python selenium
【发布时间】:2020-11-17 17:46:56
【问题描述】:
from selenium import webdriver
from bs4 import BeautifulSoup
import time
################import the chrome web driver and define the location###############
driver = webdriver.Chrome(executable_path='C:/Users/../Downloads/cd79/chromedriver.exe')
###################################################################################

###########open the web page and print the title##############
page = driver.get("https://kjustin765.wixsite.com/website")
print(driver.title)
driver.maximize_window()
time.sleep(5)
while True:
    soup = BeautifulSoup(page.content, 'html.parser')
    button1 = soup.find('span', class_='pWNha').text
    if 'Yes' in button1:
        driver.refresh()
    else:
        button1.click()

为什么page 返回为None? 这是错误

soup = BeautifulSoup(page.content, 'html.parser')
AttributeError: 'NoneType' object has no attribute 'content

【问题讨论】:

  • BeautifulSoup(driver.page_source, 'html.parser')

标签: python-3.x selenium beautifulsoup


【解决方案1】:

要获得正确的数据,请使用page.page_source 而不是page.content

soup = BeautifulSoup(page.page_source, 'html.parser')

.content 方法来自requests 库,如果您使用它来请求页面。例如:

import requests
page = requests.get(my_url).content

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2017-05-01
    • 2020-10-03
    • 1970-01-01
    • 2017-10-21
    • 2019-04-18
    相关资源
    最近更新 更多