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()
使用python selenium实现url不变的情况下,翻页爬取页面数据
于 2023-03-06 15:35:56 首次发布