如何使用python从input.txt文件中传递参数值?

问题描述 投票:0回答:1

我想要从 input.txt 文件传递

params
中的动态值?

输入.txt 文件:-
术语 = ditech 流程解决方案,国家 = IN,操作 = get_search_companies

对于此代码:-

import requests
from bs4 import BeautifulSoup


def read_params(file_path):
    params = {}
    with open(file_path, 'r') as file:
        for line in file:
            key, value = line.strip().split('=')
            params[key] = value
    return params


api_url = "https://lei-registrations.in/wp/wp-admin/admin-ajax.php"

input_file_path = "input.txt"

params = read_params(input_file_path)

# manual using params
# params = {
#     "term": "ditech process solutions",  # <-- search term
#     "country": "IN",
#     "action": "get_search_companies",
# }

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0"
}

data = requests.get(api_url, params=params, headers=headers).json()

if data["success"]:
    soup = BeautifulSoup(data["data"], "html.parser")
    for r in soup.select(".searchResults_title"):
        name = r.select_one(".searchResults_name").text
        number = r.select_one(".searchResults_number").text

        print(f"{name:<50} {number}")
python python-requests request
1个回答
0
投票

文本文件:

{
    "term": "ditech process solutions",
    "country": "IN",
    "action": "get_search_companies",
}

读取文件的代码:

import json

input_file_path = "input.txt"

with open(input_file_path) as json_data:
    params = json.load(json_data)
© www.soinside.com 2019 - 2024. All rights reserved.