移除 Cookie

This commit is contained in:
JoeamAmier
2023-08-29 19:17:27 +08:00
parent 101b4e143e
commit 301152e0c7
4 changed files with 9 additions and 17 deletions

View File

@@ -11,6 +11,7 @@
<h1>使用示例</h1> <h1>使用示例</h1>
<pre> <pre>
# 测试链接 # 测试链接
error_demo = "https://www.xiaohongshu.com/explore/"
image_demo = "https://www.xiaohongshu.com/explore/64d1b406000000000103ee8d" image_demo = "https://www.xiaohongshu.com/explore/64d1b406000000000103ee8d"
video_demo = "https://www.xiaohongshu.com/explore/64c05652000000000c0378e7" video_demo = "https://www.xiaohongshu.com/explore/64c05652000000000c0378e7"
# 实例对象 # 实例对象
@@ -21,18 +22,17 @@ headers = {
} # 请求头 } # 请求头
proxies = None # 代理 proxies = None # 代理
timeout = 10 # 网络请求超时限制默认值10 timeout = 10 # 网络请求超时限制默认值10
cookie = "" # 小红书网页 cookie无需登录获取数据失败时可以尝试手动设置
xhs = XHS( xhs = XHS(
path=path, path=path,
folder=folder, folder=folder,
headers=headers, headers=headers,
proxies=proxies, proxies=proxies,
timeout=timeout, timeout=timeout,) # 使用自定义参数
cookie=cookie) # 使用自定义参数
# xhs = XHS() # 使用默认参数 # xhs = XHS() # 使用默认参数
# 无需区分图文和视频作品 # 无需区分图文和视频作品
# 返回作品详细数据,包括下载地址 # 返回作品详细数据,包括下载地址
download = True # 启用自动下载作品文件 download = True # 启用自动下载作品文件
print(xhs.extract(error_demo)) # 获取数据失败时返回空字典
print(xhs.extract(image_demo, download=download)) print(xhs.extract(image_demo, download=download))
print(xhs.extract(video_demo, download=download)) print(xhs.extract(video_demo, download=download))
</pre> </pre>

View File

@@ -15,14 +15,12 @@ def example():
} # 请求头 } # 请求头
proxies = None # 代理 proxies = None # 代理
timeout = 10 # 网络请求超时限制默认值10 timeout = 10 # 网络请求超时限制默认值10
cookie = "" # 小红书网页 cookie无需登录获取数据失败时可以尝试手动设置
xhs = XHS( xhs = XHS(
path=path, path=path,
folder=folder, folder=folder,
headers=headers, headers=headers,
proxies=proxies, proxies=proxies,
timeout=timeout, timeout=timeout, ) # 使用自定义参数
cookie=cookie) # 使用自定义参数
# xhs = XHS() # 使用默认参数 # xhs = XHS() # 使用默认参数
# 无需区分图文和视频作品 # 无需区分图文和视频作品
# 返回作品详细数据,包括下载地址 # 返回作品详细数据,包括下载地址

View File

@@ -5,14 +5,14 @@ from requests import get
class Download: class Download:
chunk = 262144
def __init__( def __init__(
self, self,
path, path,
folder, folder,
headers: dict, headers: dict,
proxies=None, ): proxies=None,
chunk=256 * 1024, ):
self.root = self.init_root(path, folder) self.root = self.init_root(path, folder)
self.headers = self.init_headers(headers) self.headers = self.init_headers(headers)
self.proxies = { self.proxies = {
@@ -20,6 +20,7 @@ class Download:
"https": proxies, "https": proxies,
"ftp": proxies, "ftp": proxies,
} }
self.chunk = chunk
@staticmethod @staticmethod
def init_headers(headers: dict) -> dict: def init_headers(headers: dict) -> dict:
@@ -47,4 +48,4 @@ class Download:
f.write(chunk) f.write(chunk)
print(f"{name} 下载成功!") print(f"{name} 下载成功!")
except exceptions.ChunkedEncodingError: except exceptions.ChunkedEncodingError:
print("网络异常,下载文件失败!") print(f"网络异常,{name} 下载失败!")

View File

@@ -13,7 +13,6 @@ class XHS:
headers = { headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
"Referer": "https://www.xiaohongshu.com/", "Referer": "https://www.xiaohongshu.com/",
"Cookie": "abRequestId=27dafe41-28af-5b33-9f22-fe05d8c4ac2f; xsecappid=xhs-pc-web; a1=18a363d90c9gw7eaz2krqhj4cx2gtwgotul1wur8950000289463; webId=27fb29ed7ff41eadd4bc58197a465b63; websectiga=cffd9dcea65962b05ab048ac76962acee933d26157113bb213105a116241fa6c; sec_poison_id=3a1e34ee-3535-4ee9-8186-4d574da5291e; web_session=030037a3d84590608f6da85793234a9a6588ed; gid=yY0qKqfd2Y9qyY0qKqfj877FSjkEWd0uJTFA1YjxV4SCJy28k9EklE888JYj4Kq82242dKiY; webBuild=3.6.0; cache_feeds=[]",
} }
links = compile(r"https://www.xiaohongshu.com/explore/[0-9a-z]+") links = compile(r"https://www.xiaohongshu.com/explore/[0-9a-z]+")
@@ -23,19 +22,13 @@ class XHS:
folder="Download", folder="Download",
headers=None, headers=None,
proxies=None, proxies=None,
timeout=10, timeout=10):
cookie=None):
self.set_cookie(cookie)
self.html = Html(headers or self.headers, proxies, timeout) self.html = Html(headers or self.headers, proxies, timeout)
self.image = Image() self.image = Image()
self.video = Video() self.video = Video()
self.explore = Explore() self.explore = Explore()
self.download = Download(path, folder, self.html.headers, proxies) self.download = Download(path, folder, self.html.headers, proxies)
def set_cookie(self, cookie: str):
if cookie:
self.headers["Cookie"] = cookie
def get_image(self, container: dict, html: str, download): def get_image(self, container: dict, html: str, download):
urls = self.image.get_image_link(html) urls = self.image.get_image_link(html)
if download: if download: