有python输入问题

发布于 2025-02-13 19:43:37 字数 1234 浏览 1 评论 0 原文

我的 python 代码如下所示。基本上,我使用 urlib 的模块加入URL的两个部分。我面临的问题是在URL加入我的输出时看起来像下面。如下所示,从A的A列表中显示的输入在URL的开始部分显示,End具有启动信息。我的预期输出也如下所述。

总而言之,我希望用户输入条款总数,并且应将输入的条款传递到URL的查询部分(即QUERY [] =“”& query [] =“”)。不知道我是否缺少什么。

预先感谢您的帮助!

代码

from urllib.parse import urljoin

num_terms=int(input("Enter total number of search terms:")) #Asking user for number of terms

a=input("Enter all search terms: ").split(",",num_terms) #User enters all the terms

start,end=input("Enter start and end date").split() #User enters start and end date

base_url="http://mytest.org"
join_url="/comments/data?"+"terms[]={}"+"&terms[]={}"*int(num_terms-1)+"&start={}&end={}".format(a,start,end)

url=urljoin(base_url,join_url) #Joining url
url

输出:

Enter total number of search terms:3
Enter all search terms: ty ou io
Enter start and end date2345 7890
"http://mytest.org/comments/data?terms[]={}&terms[]={}&terms[]={}start=['ty ou io']&end=2345"

预期输出

"http://mytest.org/comments/data?terms[]=ty&terms[]=ou&terms[]=io&start=2345&end=7890"

My python code looks like below. Basically, I am joining two part of url using urljoin module of urlib. The issue that I am facing is during the URL join my output looks like below. As shown below the input from a which is a list is getting displayed at start part of url and end has start information. My expected output is also mentioned below.

To summarize, I want user to input total number of terms and the entered terms should be passed into query part of URL (i.e. query[]=" "&query[]= " "). Not sure if I am missing something.

Thanks in advance for help!

Code

from urllib.parse import urljoin

num_terms=int(input("Enter total number of search terms:")) #Asking user for number of terms

a=input("Enter all search terms: ").split(",",num_terms) #User enters all the terms

start,end=input("Enter start and end date").split() #User enters start and end date

base_url="http://mytest.org"
join_url="/comments/data?"+"terms[]={}"+"&terms[]={}"*int(num_terms-1)+"&start={}&end={}".format(a,start,end)

url=urljoin(base_url,join_url) #Joining url
url

Output:

Enter total number of search terms:3
Enter all search terms: ty ou io
Enter start and end date2345 7890
"http://mytest.org/comments/data?terms[]={}&terms[]={}&terms[]={}start=['ty ou io']&end=2345"

Expected Output

"http://mytest.org/comments/data?terms[]=ty&terms[]=ou&terms[]=io&start=2345&end=7890"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事与诗 2025-02-20 19:43:37

我发现的一个问题是:搜索词没有用来拆分字符串的任何()。

# the base URL path
url_base = "http://mytest.org/comments/data?"

# you don't need a search term number here
# the split below will do the job

# ask for search item directly, must have at least one item
a = input("Enter all search terms (separate by ,): ").split(",")
while len(a) < 1:
    a = input("Enter all search terms (separate by ,): ").split(",")

# ask for the start and end dates, no guarantee they are correct
# so use loop to force the user does the check for you 
dates = input("Enter the start and end date (separate by ,): ").split(",")
while len(dates) != 2:
    dates = input("Enter the start and end date (separate by ,): ").split(",")

# form the URL 
url_search = "&".join([f"terms[]={x}" for x in a])
url_date = "start=" + dates[0] + "&end=" + dates[1]

# the final result
url_final = "&".join([url_base, url_search, url_date])

# print the result
print(url_final)

输出就像:

Enter all search terms (separate by ,): ty,ou,io
Enter the start and end date (separate by ,): 2000,2022
http://mytest.org/comments/data?&terms[]=ty&terms[]=ou&terms[]=io&start=2000&end=2022

One issue I spotted: the search terms don't have any (,) which you used to split the string.

# the base URL path
url_base = "http://mytest.org/comments/data?"

# you don't need a search term number here
# the split below will do the job

# ask for search item directly, must have at least one item
a = input("Enter all search terms (separate by ,): ").split(",")
while len(a) < 1:
    a = input("Enter all search terms (separate by ,): ").split(",")

# ask for the start and end dates, no guarantee they are correct
# so use loop to force the user does the check for you 
dates = input("Enter the start and end date (separate by ,): ").split(",")
while len(dates) != 2:
    dates = input("Enter the start and end date (separate by ,): ").split(",")

# form the URL 
url_search = "&".join([f"terms[]={x}" for x in a])
url_date = "start=" + dates[0] + "&end=" + dates[1]

# the final result
url_final = "&".join([url_base, url_search, url_date])

# print the result
print(url_final)

The output is like:

Enter all search terms (separate by ,): ty,ou,io
Enter the start and end date (separate by ,): 2000,2022
http://mytest.org/comments/data?&terms[]=ty&terms[]=ou&terms[]=io&start=2000&end=2022
感情洁癖 2025-02-20 19:43:37

请求通过将 dict 带有URL参数到 params 参数(读取传递URL中的参数

import requests

response = requests.get(
    "http://mytest.org/comments/data",
    {
        "terms[]": ["ty", "ou", "io"],
        "start": 2345,
        "end": 7890
    }
)

As author mentioned in this comment he/she will use requests to make an API call, so constructing URL isn't necessary, you can just use functionality of module you're using. You can let requests build query string internally by passing dict with URL params to params argument (read Passing Parameters In URLs):

import requests

response = requests.get(
    "http://mytest.org/comments/data",
    {
        "terms[]": ["ty", "ou", "io"],
        "start": 2345,
        "end": 7890
    }
)
猫九 2025-02-20 19:43:37

一个问题是您的代码仅格式化URL的最后位。也就是说,

"&start={}&end={}".format(a,start,end)

是应用格式化的唯一部分;您需要添加括号。

另一件事是,我们需要 unfack 代码> a ,在 .format 函数中:

join_url=("/comments/data?"+"terms[]={}"+"&terms[]={}"*int(num_terms-1)+"&start={}&end={}").format(*a,start,end)

但我建议使用 f-strings 而不是 .format :(

join_url=("/comments/data?"+'&'.join([f"terms[]={term}"for term in a])+f"&start={start}&end={end}")

我还使用 str.join 用于术语而不是字符串乘法。)

One problem is your code is only formatting the last bit of the url. That is,

"&start={}&end={}".format(a,start,end)

is the only part where the formatting applies; you need to add parentheses.

The other thing is that we need to unpack the list of terms, a, in the .format function:

join_url=("/comments/data?"+"terms[]={}"+"&terms[]={}"*int(num_terms-1)+"&start={}&end={}").format(*a,start,end)

But I'd recommend using f-strings instead of .format:

join_url=("/comments/data?"+'&'.join([f"terms[]={term}"for term in a])+f"&start={start}&end={end}")

(I also used str.join for the terms instead of string multiplication.)

画▽骨i 2025-02-20 19:43:37

一个简单的 循环应该足够:

terms = ""
for i in range(num_terms):
    terms += f"terms[]={a[i]}&"

基本上,格式采用一个值,它不会按照您想要的列表迭代。这是实现目标的简单方法。您也可以使用列表理解。

[f"terms[]={term}"for term in a]

输出:

Enter total number of search terms:3
Enter all search terms: au,io,ua
Enter start and end date233 444
http://mytest.org/comments/data?terms[]=au&terms[]=io&terms[]=ua&&start=233&end=444

A simple for loop should suffice:

terms = ""
for i in range(num_terms):
    terms += f"terms[]={a[i]}&"

Basically, format takes a single value, it does not iterate over a list as you wanted. This is a simple way to achieve your goal. You could probably use list comprehension as well.

[f"terms[]={term}"for term in a]

Output:

Enter total number of search terms:3
Enter all search terms: au,io,ua
Enter start and end date233 444
http://mytest.org/comments/data?terms[]=au&terms[]=io&terms[]=ua&&start=233&end=444
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文