当前位置:   article > 正文

使用python selenium实现url不变的情况下,翻页爬取页面数据_python爬虫翻页url不变

python爬虫翻页url不变
import requests
from bs4 import BeautifulSoup
import openpyxl
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

wt = openpyxl.Workbook()
ws = wt.create_sheet('sheet11')
ws.append(['LIST'])
browser = webdriver.Edge()
url = 'https://www.freebuf.com/articles/security-management'
html = browser.get(url)
# print(browser.page_source)
nextpagebutton = browser.find_element(By.XPATH, "//li[@class = ' ant-pagination-next']/a")
for r in range(1,16):
    soup = BeautifulSoup(browser.page_source, 'html.parser')
    spans = soup.find_all('span', class_ = 'title text-line-1')
    browser.execute_script("arguments[0].click();" ,nextpagebutton)
    time.sleep(1)


    print('--------------------正在打印第' + str(r) + '页数据---------------------------')
    for span in spans:
        LIST = span.text.strip()
        print(LIST)
        ws.append([LIST])

print('success')
browser.close()
wt.save('demo.xlsx')
wt.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/489030
推荐阅读
相关标签
  

闽ICP备14008679号