from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options
import csv
import requests
opt = Options()
opt.add_argument(‘—headless’)
opt.add_argument(‘—disable-gpu’)
web = Chrome(options=opt)
web.get(‘https://gamepassport.net/‘)
# 以下 实现内嵌浏览器下滑操作 先定位到属性
for i in range(500, 100000, 500):
js = f”var q=document.getElementById(‘content’).scrollTop={i}”
web.execute_script(js)
time.sleep(3)
headers = {
‘user-agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36’
}
baocundizhi = ‘./Xbox/‘
with open(‘Xbox游戏信息.csv’, ‘a’, encoding=’utf-8’, newline=’’) as f:
csv_writer = csv.writer(f)
csv_writer.writerow([“游戏名称”, “游戏类型”, “上映时间”, “完成时间”, “图片链接”])
# 获取全部div
div_list = web.find_elements_by_xpath(‘//*[@id=”content”]/div’)
# 循环提取内容
n = 1
for div in div_list:
# 替换掉原有的: Win系统里:路径分隔符,属于系统保留字符,所以不能用来作为文件名
title = div.find_element_by_xpath(‘./div/h6’).text.replace(‘:’, ‘-‘)
leixing = div.find_element_by_xpath(‘./div/div/div[1]’).text
syshijian = div.find_element_by_xpath(‘./div/div/div[2]/span[1]’).text
wcshijian = div.find_element_by_xpath(‘./div/div/div[2]/span[2]’).text
tupianlianjie = div.find_element_by_xpath(‘./a/picture/img’).get_attribute(‘src’)
pinjie = baocundizhi + ‘%s’ % n + ‘.’ + title + ‘.jpg’
with open('Xbox游戏信息.csv', 'a', encoding='utf-8', newline='') as f:<br /> csv_writer = csv.writer(f)<br /> csv_writer.writerow([title, leixing, syshijian, wcshijian, tupianlianjie])
print(title + "保存成功")<br /> # 可能会因为网速问题出现图片未加载情况<br /> try:<br /> f = open(pinjie, mode='wb')<br /> f.write(requests.get(url=tupianlianjie, headers=headers).content) # 图片、文件 content<br /> print(title + '图片保存成功!!!')<br /> except:<br /> print("图片未加载")<br /> n += 1<br /> time.sleep(0.5)<br />web.quit()