我一直在做研究,到目前爲止,我發現了我打算使用它的scrapy的python包,現在我正試圖找出構建刮板的好方法使用scrapy抓取無限滾動的網站。挖掘周圍後,我發現有一個包調用硒,它有python模塊。我有一種感覺,有人已經使用Scrapy和Selenium來做無限滾動的網站。如果有人能夠指出一個例子,那將會很棒。使用python無限滾動的抓取網站
2
A
回答
1
from selenium.webdriver.common.keys import Keys
import selenium.webdriver
driver = selenium.webdriver.Firefox()
driver.get("http://www.something.com")
lastElement = driver.find_elements_by_id("someId")[-1]
lastElement.send_keys(Keys.NULL)
這將打開一個頁面,找到與給定id
和滾動該元素進入視野最底層的元素。當頁面加載更多時,您將不得不查詢驅動程序以獲取最後一個元素,而且我發現這樣做會很慢,因爲頁面變大。時間主要由driver.find_element_*
調用,因爲我不知道如何顯式查詢頁面中的最後一個元素。
通過實驗你可能會發現有一個上限,以元素的量在頁面加載動態的,如果你寫的東西,裝這個數字,然後纔打了一個電話給driver.find_element_*
這將是最好的。
1
你可以使用硒來取消像twitter或facebook這樣的無限滾動網站。
步驟1:使用PIP
pip install selenium
步驟2安裝硒:使用下面的代碼來自動無限滾動和提取源代碼
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import sys
import unittest, time, re
class Sel(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "https://twitter.com"
self.verificationErrors = []
self.accept_next_alert = True
def test_sel(self):
driver = self.driver
delay = 3
driver.get(self.base_url + "https://stackoverflow.com/search?q=stackoverflow&src=typd")
driver.find_element_by_link_text("All").click()
for i in range(1,100):
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(4)
html_source = driver.page_source
data = html_source.encode('utf-8')
if __name__ == "__main__":
unittest.main()
for循環允許通過所述解析無限滾動和後期,你可以提取加載的數據。
步驟3:根據需要打印數據。
相關問題
- 1. scle網站無限滾動
- 2. 如何抓取無限滾動頁面?
- 3. 使用python來抓取一個網站
- 4. 如何正確抓取無限滾動的網頁?
- 5. 使用無限滾動頁面的抓取工具
- 6. 抓取整個網站python
- 7. 訪問使用無限動態滾動的網站元素
- 8. 使用動態頁面抓取網站
- 9. 抓取網站使用PHP
- 10. 使用Python無法抓取網頁
- 11. Python網站用BeautifulSoup抓取文本
- 12. 使用wget抓取網站並限制已抓取鏈接的總數
- 13. 使用requests.get方法無限滾動python
- 14. 網站抓取使用zombie.js的問題
- 15. 使用python從網站抓取多個網頁
- 16. 抓取網站和動態網址
- 17. 谷歌無法抓取我的網站
- 18. 使用Python抓取網站後獲取特定數據
- 19. scrapy抓取網站
- 20. 我如何從Python網站抓取網站上的pdf鏈接
- 21. 使用Scrapy遞增抓取網站
- 22. 使用Angular js抓取網站
- 23. 順序抓取網站使用scrapy
- 24. 使用xcode從網站抓取文本
- 25. 建議使用抓取網站內容
- 26. 使用curl和xpath來抓取網站
- 27. 如何使用scrapy抓取網站?
- 28. 安裝抓取,網站抓取庫
- 29. 網站抓取器每隔幾秒就抓取一次網站
- 30. 使用Python/Pexpect的抓取網絡
一種方法是觸發一些向下箭頭鍵讓瀏覽器向下滾動。 – donfuxx
看一下:http://stackoverflow.com/questions/17975471/selenium-with-scrapy-for-dynamic-page – alecxe