簡體   English   中英

Python使用Selenium和Beautiful Soup抓取JavaScript

[英]Python Scraping JavaScript using Selenium and Beautiful Soup

我正在嘗試使用BS和Selenium抓取JavaScript啟用頁面。 到目前為止,我有以下代碼。 它仍然無法以某種方式檢測到JavaScript(並返回空值)。 在這種情況下,我嘗試在底部刮取Facebook評論。 (檢查元素將類顯示為postText)
謝謝您的幫助!

from selenium import webdriver  
from selenium.common.exceptions import NoSuchElementException  
from selenium.webdriver.common.keys import Keys  
import BeautifulSoup

browser = webdriver.Firefox()  
browser.get('http://techcrunch.com/2012/05/15/facebook-lightbox/')  
html_source = browser.page_source  
browser.quit()

soup = BeautifulSoup.BeautifulSoup(html_source)  
comments = soup("div", {"class":"postText"})  
print comments

您的代碼中有一些錯誤已在下面修復。 但是,類“ postText”必須存在於其他位置,因為它沒有在原始源代碼中定義。 我對您代碼的修訂版本已通過測試,並且可以在多個網站上使用。

from selenium import webdriver  
from selenium.common.exceptions import NoSuchElementException  
from selenium.webdriver.common.keys import Keys  
from bs4 import BeautifulSoup

browser = webdriver.Firefox()  
browser.get('http://techcrunch.com/2012/05/15/facebook-lightbox/')  
html_source = browser.page_source  
browser.quit()

soup = BeautifulSoup(html_source,'html.parser')  
#class "postText" is not defined in the source code
comments = soup.findAll('div',{'class':'postText'})  
print comments

暫無
暫無

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

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