# -*- coding: utf-8 -*-
from selenium import webdriver
import time
if __name__ == "__main__":
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://www.baidu.com')
#通过ID找网页的标签,找到搜索框的标签
seek_input = driver.find_element_by_id("kw")
#设置搜索的内容
seek_input.send_keys("2018财税新政策")
#找到搜索文档按钮
seek_but = driver.find_element_by_id("su")
#并点击搜索 按钮
seek_but.click()
#并点击搜索 按钮
js = 'document.documentElement.scrollTop=10000'#拖动滚动条到底部
time.sleep(1)
driver.execute_script(js)
time.sleep(1)
total = 0 #页面数
is_next_page = True #存在下一页
page_num = 1 #要点击的页面号
#往后翻页
while is_next_page:
try:
total=total+1
if total == 1 :
result = driver.find_element_by_xpath("//a[@class='n']")
text=result.get_attribute('text')
if text.find('下一页')>=0 :
result.click()
time.sleep(2)
driver.execute_script(js)
#print('第'+total+'页')
else :
#result = driver.find_element_by_xpath("//a[@class='n']")
result = driver.find_element_by_link_text("下一页>")
result.click()
time.sleep(2)
driver.execute_script(js)
print(result)
#for each_result in result:
# text=each_result.get_attribute('text')
# if text.find('下一页')>=0 :
# each_result.click()
# time.sleep(2)
# driver.execute_script(js)
#print('第'+total+'页')
except:
print("到最后一页了")
break
#思路很简单,不断获取下一页标签,触发点击事件,不断翻页, 我测的是翻到70页,文章仅作小白学习笔记,想要获取每 #页的 链接地址下载内容可以参考我的上一篇博文
python3 学习2(分页翻看百度搜索结果)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhaofuqiangmycomm/article/details/83017269
猜你喜欢
转载自blog.csdn.net/zhaofuqiangmycomm/article/details/83017269
今日推荐
周排行