Selenium Python网络抓取“ Nonetype对象”没有属性“ div”

时间:2020-04-24 00:19:40

标签: python selenium-webdriver beautifulsoup

我是网络爬虫的新手。我正在尝试使用漂亮的汤和硒从动态加载的网站上抓取数字。

这是我的代码:

from selenium import webdriver
import bs4
from bs4 import BeautifulSoup

driver=webdriver.Chrome()
driver.get('https://roobet.com/crash')
html = driver.page_source
soup = BeautifulSoup(html,features="html5lib")
crash = soup.find('div', class_='CrashHistory_2YtPR')
num = crash.div.text
num.replace('x','')
print(num)

当我在命令提示符下使用'''python testcrash.py'''运行此程序时。

DevTools listening on ws://127.0.0.1:7628/devtools/browser/fb52b5de-1423-488a-a4dd-8442deff01d2
[34012:34760:0423/201837.291:ERROR:browser_switcher_service.cc(238)] XXX Init()
Traceback (most recent call last):
  File "testcrash.py", line 10, in <module>
    num = crash.div.text
AttributeError: 'NoneType' object has no attribute 'div'

但是当我在python应用程序中逐行运行代码时,我得到了我所需要的。这就是它的样子

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> import bs4
>>> from bs4 import BeautifulSoup
>>> driver=webdriver.Chrome()

DevTools listening on ws://127.0.0.1:4917/devtools/browser/887f1d53-d7d1-4a56-929b-bf46e13cff9d
[8592:31756:0423/171506.777:ERROR:browser_switcher_service.cc(238)] XXX Init()
>>>
>>> driver.get('https://roobet.com/crash')
>>> html = driver.page_source
>>> soup = BeautifulSoup(html,features="html5lib")
>>> crash = soup.find('div', class_='CrashHistory_2YtPR')
>>> num = crash.div.text
>>> num
'2.38x'
>>>

以下是我要抓取的页面和对象的屏幕截图:

enter image description here

0 个答案:

没有答案