[問題] 換頁抓取
各位大大好:
不好意思,剛剛發文方式弄錯,自己刪文。
小弟是一個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
→
03/10 02:02, , 1F
03/10 02:02, 1F
推
03/10 21:11, , 2F
03/10 21:11, 2F
→
03/10 21:12, , 3F
03/10 21:12, 3F
推
03/10 21:18, , 4F
03/10 21:18, 4F
→
03/11 15:29, , 5F
03/11 15:29, 5F
→
03/11 15:29, , 6F
03/11 15:29, 6F
Python 近期熱門文章
PTT數位生活區 即時熱門文章