簡體   English   中英

Python 抓取不返回任何內容

[英]Python scraping returns 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM