我正试图通过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
作为答复。有办法解决吗?
答案 0 :(得分:2)
网页由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>