作者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://webptt.com/cn.aspx?n=bbs/Python/M.1489054407.A.4BB.html
1F:→ rs6000: 建议用 pastebin.com 贴程式码 03/10 02:02
2F:推 ntumath: 问题出在於你按下按钮资料还没读进来就抓了 03/10 21:11
3F:→ ntumath: 再按下按钮後再sleep几秒看看,以後可以用wait试试看 03/10 21:12
4F:推 MinaseNayuki: 建议等5~10秒,不要抓太快 03/10 21:18
5F:→ wk9282000: 让他暂停一下後,我的问题已经顺利解决了,感谢各位大 03/11 15:29
6F:→ wk9282000: 大 03/11 15:29