我的 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"
发布评论
评论(4)
我发现的一个问题是:搜索词没有用来拆分字符串的任何()。
输出就像:
One issue I spotted: the search terms don't have any (,) which you used to split the string.
The output is like:
如请求通过将
dict
带有URL参数到params
参数(读取传递URL中的参数):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 letrequests
build query string internally by passingdict
with URL params toparams
argument (read Passing Parameters In URLs):一个问题是您的代码仅格式化URL的最后位。也就是说,
是应用格式化的唯一部分;您需要添加括号。
另一件事是,我们需要 unfack 代码> a ,在
.format
函数中:但我建议使用
f-strings
而不是.format
:(我还使用
str.join
用于术语而不是字符串乘法。)One problem is your code is only formatting the last bit of the url. That is,
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:But I'd recommend using
f-strings
instead of.format
:(I also used
str.join
for the terms instead of string multiplication.)一个简单的
循环应该足够:
基本上,
格式
采用一个值,它不会按照您想要的列表迭代。这是实现目标的简单方法。您也可以使用列表理解。输出:
A simple
for
loop should suffice: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.Output: