import requests
import csv
from lxml import etree
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.0.10191 SLBChan/105"
}
move_title = ["电影名", "出产地", "电影时长", "上映时间", "封面网址"]
with open("moves.csv", "w", encoding="utf_8", newline="") as f:
w = csv.writer(f)
w.writerow(move_title)
for i in range(1, 3):
url = "https://ssr3.scrape.center" + f"/page/{i}"
response = requests.get(url, headers=headers, auth=("admin", "admin")) # auth=():这是一个包含用户名和密码的元组,用于登录或验证用户
# 解析获取的响应
html = etree.HTML(response.content.decode()) # 字节串(bytes),而 .decode() 方法是将其转换为字符串(str)。
# 获取li标签 分组
div_list = html.xpath('//div[@class="el-col el-col-18 el-col-offset-3"]/div')
for li in div_list:
name = li.xpath('.//div/div/div[2]/a/h2/text()')[0]
addr = li.xpath('.//div[@class="el-row"]/div[2]/div[2]/span[1]/text()')[0]
time_long = li.xpath('.//div[@class="el-row"]/div[2]/div[2]/span[3]/text()')[0]
time_agen = li.xpath('.//div[@class="el-row"]/div[2]/div[3]/span/text()')[0] if len(
li.xpath('.//div[@class="el-row"]/div[2]/div[3]/span/text()')) > 0 else None
img_url = li.xpath('.//div[@class="el-row"]/div[1]/a/img/@src')[0]
# 获取封面
responses = requests.get(img_url, headers=headers)
filepath = r'./dyimg/' + name + ".png"
with open(filepath, "wb") as f:
f.write(responses.content)
#print(filepath)
move_info = [name, addr, time_long, time_agen, img_url]
w.writerow(move_info)
print("ok")
1.requests 爬取(多页)文字和图片并保存为csv文件
最新推荐文章于 2024-09-03 17:37:40 发布