2.读取不同text1和text2文件中的内容,获取不同cookie1和cookie2信息,爬取相关网页图片,并存入本地文件image1和image2 Python代码
时间: 2024-10-29 08:11:15 浏览: 33
Cookie的存入和读取,并解决中文问题
在Python中,你可以使用requests库来处理HTTP请求,BeautifulSoup库解析HTML内容,以及os库来操作文件。下面是一个简单的示例,假设你已经有了`text1`和`text2`文件,里面存储了需要的cookies(例如,它们可能包含网站的登录凭据),然后你需要访问两个不同的页面以下载图片:
```python
import requests
from bs4 import BeautifulSoup
import os
# 假设你有读取文本文件的函数,这里省略
def read_cookies(file_name):
with open(file_name, 'r') as f:
cookies = f.read()
return cookies
# 定义要抓取的URL
url1 = "http://example.com/image1"
url2 = "http://example.com/image2"
# 获取cookies
cookie1 = read_cookies('cookie1')
cookie2 = read_cookies('cookie2')
# 使用cookies发起请求
response1 = requests.get(url1, cookies={'cookie_key': cookie1})
response2 = requests.get(url2, cookies={'cookie_key': cookie2})
# 检查请求是否成功
if response1.status_code == 200 and response2.status_code == 200:
# 解析响应中的图片链接
soup1 = BeautifulSoup(response1.content, 'html.parser')
img_url1 = soup1.find('img')['src'] # 假设图片在img标签里
soup2 = BeautifulSoup(response2.content, 'html.parser')
img_url2 = soup2.find('img')['src']
# 下载并保存图片
local_path1 = 'image1.jpg'
if not os.path.exists(local_path1):
with open(local_path1, 'wb') as f:
f.write(requests.get(img_url1).content)
local_path2 = 'image2.jpg'
if not os.path.exists(local_path2):
with open(local_path2, 'wb') as f:
f.write(requests.get(img_url2).content)
else:
print(f"Failed to fetch {url1} or {url2}")
阅读全文