Python 抓取返回 none



我正在尝试从带有BeautifulSoup的HTML页面中取一个名称:

import urllib.request
from bs4 import BeautifulSoup
nightbot = 'https://nightbot.tv/t/tonyxzero/song_requests'
page = urllib.request.urlopen(nightbot)
soup = BeautifulSoup(page, 'html5lib')
list_item = soup.find('strong', attrs={'class': 'ng-binding'})
print (list_item)

但是当我打印print(list_item)时,我收到none作为回复。有办法解决它吗?

网页由 javascript 渲染。所以你必须使用像selenium这样的包来得到你想要的东西。

你可以试试这个:

法典:

import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://nightbot.tv/t/tonyxzero/song_requests')
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
list_item = soup.find('strong', attrs={'class': 'ng-binding'})
print (list_item)

结果:

<strong class="ng-binding" ng-bind="$state.current.title">Song Requests: TONYXZERO</strong>

最新更新