在Python中将字符串添加到URL

4

我正在尝试在URL的中间添加一个字符串。不知何故,我的输出结果看起来像这样:

http://www.Holiday.com/('Woman',)/Beach
http://www.Holiday.com/('Men',)/Beach

不知何故,它应该看起来像这样:

http://www.Holiday.com/Woman/Beach
http://www.Holiday.com/Men/Beach

我正在使用的代码如下所示:
list = {'Woman','Men'}
url_test = 'http://www.Holiday.com/{}/Beach'
for i in zip(list):
    url = url_test.format(str(i))
    print(url)

你看到了什么被打印出来的内容? - m-ric
1
请不要将 list 作为变量名。 - Rahul K P
3个回答

6

快要完成了,只是不需要使用zip

items = {'Woman','Men'} # notice that this is a `set` and not a list
url_test = 'http://www.Holiday.com/{}/Beach'
for i in items:
    url = url_test.format(i)
    print(url)
< p > zip 函数的目的是通过其索引将多个集合连接在一起。当 zip 从每个集合中连接值时,它们会被放置在一个元组中,并且其 __str__ 表示形式正是您所得到的内容。在这里,您只需要迭代集合中的项目。


3
值得一提的是,“list”不是一个好的变量名,而且你删除的“str”是不必要的。 - brianpck
1
@brianpck - 真的没错 :) 尤其是它实际上是一个set - Gilad Green
1
@GiladGreen,非常感谢,这正是我在寻找的东西。不知何故,我误解了zip的用法。 - MCM

2
您也可以尝试这个方法,但请勿将“list”用作变量名。
lst = {'Woman','Men'}
url_test = 'http://www.Holiday.com/%s/Beach'
for i in lst:
     url = url_test %i
     print url

0
from urllib.request import urlopen
from bs4 import BeautifulSoup as BS
url = "https://www.imdb.com/chart/top?ref_=nv_mv_250"
html = urlopen(url)

url_list = BS(html, 'lxml')
type(url_list)

all_links = url_list.find_all('a', href=re.compile("/title/tt"))
for link in all_links:
  print(link.get("href"))
all_urls = link.get("href")

url_test = 'http://www.imdb.com/{}/'
for i in all_urls:
    urls = url_test.format(i)
    print(urls)

    this is the code to scrape the urls of all the 250 movies from the main url.
    but the code gives the result as ------

    http://www.imdb.com///
    http://www.imdb.com/t/
    http://www.imdb.com/i/
    http://www.imdb.com/t/
    http://www.imdb.com/l/
    http://www.imdb.com/e/
    http://www.imdb.com///
    and so on ...

    how can i split 'all_urls' using a comma, or how can I make a list of urls in 
    'all_urls'....

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接