简体   繁体   中英

Python scraping returns none

I'm trying to take a name from a HTML page with BeautifulSoup:

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. There is a way to fix it?

Webpage is rendered by javascript. So you have to use a package like selenium to get what you want.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM