# -*- coding: utf-8 -*-
# 猫眼数据
import random
import time
from openpyxl import workbook
from selenium import webdriver
from selenium.webdriver.common.by import By
url = "https://www.maoyan.com/board/4?offset=0"
# driver.find_element(By.ID, 'kw').send_keys('小奕')
# driver.find_element(By.CLASS_NAME, "bg.s_btn").click()
options = webdriver.ChromeOptions()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get(url)
# driver.maximize_window()
workbook = workbook.Workbook()
sheet = workbook.worksheets[0]
table_title = ["排名", "电影名称", "主演", "上映时间", "评分"]
for i, item in enumerate(table_title, 1):
cell = sheet.cell(1, i)
cell.value = item
def get_data():
dds = driver.find_elements(By.XPATH, '//*[@id="app"]/div/div/div[1]/dl/dd')
# print(dds)
# print(type(dds))
for j in dds:
data = j.text.split("\n")
print(data)
li = [
data[0].strip(),
data[1].strip(),
data[2].strip(),
data[3].strip(),
data[4].strip()
]
print(li)
# print(type(li))
max_row = sheet.max_row
for h, items in enumerate(li, 1):
cells = sheet.cell(max_row + 1, h)
cells.value = items
workbook.save("maoyan.xlsx")
while True:
get_data()
try:
time.sleep(random.randint(2, 3))
driver.find_element(By.LINK_TEXT, "下一页").click()
except Exception as e:
print(e)
print("爬取完成")
driver.quit()
break
Python自动化驱动实战
于 2023-04-24 19:49:19 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)