[英]Python scraping returns none
I'm trying to take a name from a HTML page with BeautifulSoup:我正在尝试使用 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)
But when i print print(list_item)
i get a none
as reply.但是当我打印print(list_item)
我得到了一个none
作为回复。 There is a way to fix it?有办法解决吗?
Webpage is rendered by javascript.网页由 javascript 呈现。 So you have to use a package like selenium
to get what you want.所以你必须使用像selenium
这样的包来得到你想要的。
You can try this:你可以试试这个:
CODE:代码:
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)
RESULT:结果:
<strong class="ng-binding" ng-bind="$state.current.title">Song Requests: TONYXZERO</strong>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.