作者wk9282000 (遺忘天空)
看板Python
標題[問題] 換頁抓取
時間Thu Mar 9 18:13:24 2017
各位大大好:
不好意思,剛剛發文方式弄錯,自己刪文。
小弟是一個python新手,也是第一次在PTT發文。
最近在嘗試抓取華視新聞搜尋"食品安全"後的標題
我參考了以下的影片去撰寫我需要的爬蟲程式:
https://www.youtube.com/watch?v=MQH4Rau_F_A&index=10&list=PLohb4k71XnPaQRTvKW4
Uii1oq-JPGpwWF&t=354s
但在換頁的時候卻沒辦法順利換頁進行下一頁的標題抓取
呈現的結果都是第一頁的內容重覆抓取
請問各位大大有什麼解決方法?
以下為程式碼:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import requests
from bs4 import BeautifulSoup
import time
driver =
webdriver.PhantomJS(executable_path='C:/Users/Jerry/Desktop/phantomjs-2.1.1-windows/bin/phantomjs')
driver.get('
http://news.cts.com.tw/search.php?q=%E9%A3%9F%E5%93%81%E5%AE%89%E5%85%A8')
time.sleep(3)
soup = BeautifulSoup(driver.page_source)
for link in
soup.select('.gs-title.gsc-table-cell-thumbnail.gsc-thumbnail-left a'):
newtitle = link.text
print(newtitle)
driver.find_element_by_xpath("//div[@id='cse']/div/div/div/div[5]/div[2]/div/div/div[2]/div[11]/div/div[2]").click()
soup = BeautifulSoup(driver.page_source)
for link in
soup.select('.gs-title.gsc-table-cell-thumbnail.gsc-thumbnail-left a'):
newtitle = link.text
print(newtitle)
driver.close()
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.123.162.157
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1489054407.A.4BB.html
→ rs6000: 建議用 pastebin.com 貼程式碼 03/10 02:02
推 ntumath: 問題出在於你按下按鈕資料還沒讀進來就抓了 03/10 21:11
→ ntumath: 再按下按鈕後再sleep幾秒看看,以後可以用wait試試看 03/10 21:12
推 MinaseNayuki: 建議等5~10秒,不要抓太快 03/10 21:18
→ wk9282000: 讓他暫停一下後,我的問題已經順利解決了,感謝各位大 03/11 15:29
→ wk9282000: 大 03/11 15:29