Cách khởi chạy CookieRobot bằng Python
Bài viết này được dịch tự động từ tiếng Anh. Chúng tôi sẽ hiệu đính và hoàn thiện nó sớm!
CookieRobot giúp hồ sơ của bạn trông hợp lệ hơn bằng cách tự động thu thập cookie từ các trang web bạn đã chọn – không cần phải thu thập thủ công! Trong hướng dẫn này, chúng tôi sẽ hướng dẫn bạn cách sử dụng CookieRobot để đơn giản hóa quy trình.
Bạn đã quen với tự động hóa hoặc đã hoàn thành một số bước? Hãy sử dụng mục lục bên phải để chuyển thẳng đến nội dung bạn cần. Bắt đầu thôi!
Ứng dụng mã Python
Trong ví dụ này, các thiết lập được cấu hình trong đối tượng payload
, bên trong run_script()
chức năng:
- CookieRobot sẽ chạy qua tệp
cookie_robot.py
- ID hồ sơ hoạt động được đặt thành
ced16576-a67b-4ae5-8459-c07991d50f27
vàec0cf95f-b199-4b1c-b394-af1e01ac9c09
- Cả hai cấu hình sẽ chạy ở chế độ không có headless
- Các trang web là Yahoo.com và Amazon.com và chúng sẽ được truy cập theo thứ tự ngẫu nhiên
- Vì
fractionMode
được đặt thành 1 nên 100% các trang web được liệt kê sẽ được truy cập - CookieRobot sẽ tự động chấp nhận thông báo đồng ý cookie nếu chúng xuất hiện
import logging
import requests
import json
# In project root you need a file called token with your auth token in there
def setup_logging():
"""Set up logging configuration."""
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("script_runner.log"),
logging.StreamHandler()
]
)
def get_token_from_file():
"""Read the token from a file called 'token' in the project root."""
try:
with open("token", "r") as file:
token = file.read().strip()
return token
except FileNotFoundError:
logging.error("Token file not found in the project root.")
raise
except Exception as e:
logging.error("An error occurred while reading the token file: %s", str(e))
raise
def run_script():
url = "https://launcher.mlx.yt:45001/api/v1/run_script"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {get_token_from_file()}"
}
payload = {
"script_file": "cookie_robot.py",
"profile_ids": [
{
"profile_id": "ced16576-a67b-4ae5-8459-c07991d50f27",
"is_headless": False
},
# {
# "profile_id": "ec0cf95f-b199-4b1c-b394-af1e01ac9c09",
# "is_headless": False
# },
],
"script_params": [
{
"name": "websites",
"value": ["yahoo.com", "amazon.com"]
},
{
"name": "randomOrder",
"value": True
},
{
"name": "fractionMode",
"value": 1
},
{
"name": "processCookieConsent",
"value": True
}
]
}
try:
logging.info("Payload to send: %s", json.dumps(payload, indent=4))
logging.info("Sending request to the script runner endpoint...")
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=30)
if response.status_code == 200:
logging.info("Script executed successfully.")
logging.info("Response: %s", response.json())
else:
logging.error("Failed to execute script. Status code: %d", response.status_code)
logging.error("Response: %s", response.text)
except requests.exceptions.RequestException as e:
logging.error("An error occurred while making the request: %s", str(e))
if __name__ == "__main__":
setup_logging()
run_script()