当前位置:   article > 正文

Python B站魔力赏市集搜索_b站市集脚本

b站市集脚本

目录

获取heder(手动)

转到postman(获取你的header)(自动?)

代码

报错说明(一点点)

获取到感兴趣的商品后


参考:

 魔力赏市集搜索?使用python去以一个合适的价格搜到想要的手商品_哔哩哔哩_bilibili

写给自己看的,不回答问题

改进:

1. 添加了筛选词 exclude_words

2. 避免重复打印相同条目

3. 添加打印循环次数,让你知道程序还在跑(2023.12.20优化了显示效果,实现了直接刷新)

4. 简化打印,只打印id,名称,价格

5. 优化异常捕捉,跑太多会触发哔哩哔哩安全风控策略

6. 请gpt用Thread加速,不知道有没有用

  1. 获取heder(手动)

        进入网址https://mall.bilibili.com/neul-next/index.html?page=magic-market_index​

        选择“网络”,此时会发现是空白的,点击刷新,才会刷新出信息

选择“Fetch/XHR”,右键list

选择复制cURL(bash)

转到postman(获取你的header)(自动?)

在My Workspace里点击加号

https://mall.bilibili.com/mall-magic-c/internet/c2c/v2/list

粘贴刚才复制的cURL(bash),(好像直接复制上面这句即可)点击Send

选择Python-request,复制postman提供的代码,你就知道自己的header

代码

以下代码不能直接运行,你要填充自己的header

  1. from time import sleep
  2. import requests
  3. import json
  4. from concurrent.futures import ThreadPoolExecutor
  5. import sys
  6. def fetch_data(payload, headers):
  7. response = requests.post(url, headers=headers, data=payload)
  8. response.raise_for_status()
  9. return response.json()
  10. def process_item(item):
  11. c2c_items_id = item["c2cItemsId"]
  12. c2c_items_name = item["c2cItemsName"]
  13. show_price = item["showPrice"]
  14. if c2c_items_id not in seen_c2c_items_ids:
  15. seen_c2c_items_ids.add(c2c_items_id)
  16. if not any(exclude_word in c2c_items_name for exclude_word in exclude_words):
  17. print(f"\nid: {c2c_items_id}, 名称: {c2c_items_name}, 价格: {show_price}")
  18. url = "https://mall.bilibili.com/mall-magic-c/internet/c2c/v2/list"
  19. i_want = []
  20. keyword = "Thea"
  21. exclude_words = ["景品", "早濑优香", "头盔", "英灵旅装", "二次再版", "拉芙塔莉雅", "索米", "天笠缀", "镜华",
  22. "宇崎学妹想要玩", "宇崎酱想要玩耍", "炼狱杏寿郎", "噬血代码", "我推的孩子", "春野樱",
  23. "战双帕弥什", "孤独摇滚", "后藤独", "嘉然", "路人女主的养成方法", "赵灵儿", "间谍教室",
  24. "从零开始的异世界生活", "酒会观测者", "露西亚", "亚丝娜", "绯染天空", "游戏人生", "奥丁领域",
  25. ] # 不想要的关键词
  26. seen_c2c_items_ids = set()
  27. count = 0
  28. minutes = 0
  29. # Set up the ThreadPoolExecutor
  30. with ThreadPoolExecutor(max_workers=5) as executor:
  31. while True:
  32. payload = json.dumps({
  33. "priceFilters": ["40000-90000"], # 价格筛选,这里是400~900元,真实价格后面要加两个0
  34. "categoryFilter": "2312", # 类型筛选。手办:2312 模型:2066 周边:2331 3C数码:2273
  35. # "discountFilters": ["50-70"], # 折扣筛选。
  36. "sortType": "TIME_DESC",
  37. "nextId": None
  38. })
  39. headers = {
  40. # ... (你的header)
  41. }
  42. try:
  43. response = executor.submit(fetch_data, payload, headers)
  44. response = response.result() # Retrieve the result of the thread execution
  45. if response is not None:
  46. nextId = response["data"]["nextId"]
  47. items = response["data"]["data"]
  48. executor.map(process_item, items)
  49. if nextId is None:
  50. break
  51. except TypeError as e:
  52. if "'NoneType' object is not subscriptable" in str(e):
  53. sleep(2)
  54. else:
  55. print("新错误")
  56. raise # 抛出异常
  57. except requests.exceptions.HTTPError as http_err:
  58. if http_err.response.status_code == 412:
  59. print("错误号:412 由于触发哔哩哔哩安全风控策略,该次访问请求被拒绝。")
  60. if minutes % 1 == 0:
  61. print(f"睡了{minutes}分钟")
  62. sleep(30)
  63. minutes += 0.5
  64. except requests.exceptions.JSONDecodeError:
  65. print("输入参数错误,看起来是触发了哔哩哔哩安全风控策略")
  66. break
  67. count += 1
  68. sys.stdout.write("\r---{:03d}---".format(count))
  69. sys.stdout.flush()
  70. print("==============获取完成================")
  71. if not i_want:
  72. print("没有找到")
  73. else:
  74. print("\n找到期望商品:")
  75. print(i_want)
  76. cheap = min(i_want, key=lambda x: x["price"])
  77. print("\n找到便宜好货:")
  78. print(cheap)

打印结果示例(现版本策略)

打印结果示例(旧版策略)

报错说明(一点点)

错误号:412 由于触发哔哩哔哩安全风控策略,该次访问请求被拒绝。

解决方法:

  • 使用魔法改变IP(缺点:可能网络不稳定,会导致报错)
  • 多sleep,比如每50次sleep30秒?不知道引发风控的点在哪里

获取到感兴趣的商品后

复制id,在网址的itemsId替换为复制的id即可https://mall.bilibili.com/neul-next/index.html?page=magic-market_detail&noTitleBar=1&itemsId=【你的id】&from=market_index

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/467746
推荐阅读
相关标签
  

闽ICP备14008679号