Python 如何将列表中的条目转换为字符串?
这是我当前的代码:Python 如何将列表中的条目转换为字符串?,python,Python,这是我当前的代码: kwlist = [] for url in soup.find_all('a', href=True): text = url.get_text() if keywords in text: links = (url['href']) kwlist.append(links) '|'.join(kwlist) colorlist = [] for url in soup.find_all('a', href=T
kwlist = []
for url in soup.find_all('a', href=True):
text = url.get_text()
if keywords in text:
links = (url['href'])
kwlist.append(links)
'|'.join(kwlist)
colorlist = []
for url in soup.find_all('a', href=True):
text = url.get_text()
if color in text:
colorlinks = (url['href'])
colorlist.append(colorlinks)
'|'.join(color)
#finallink = any(x in kwlist for x in colorlist)
#print(finallink)
kwset = set(kwlist)
colorset = set(colorlist)
intersection = str(kwset.intersection(colorset))
print(intersection)
driver.get('https://www.supremenewyork.com/' + intersection)
这是打印输出:
{'/shop/tops-sweaters/deshwjqp5/i640qb2pu'}
但是,这是selenium正在浏览的网站:
https://www.supremenewyork.com/%7B'/shop/tops-sweaters/deshwjqp5/i640qb2pu'%7D
我需要selenium导航到:
https://www.supremenewyork.com/shop/tops-sweaters/deshwjqp5/i640qb2pu
这与我的列表中的项目相同,只是没有%7b'和“%7D”
如何将列表更改为所需的输出?
%7B
和%7D
在调用str
后,从集合中编码{
和}
字符
您需要首先从集合中“展开”值。如果您只需要交叉点中的一个条目,并且您知道它将始终包含至少一个元素,则可以将其转换为列表,然后从中获取一个元素。比如:
# Convert to list, then take the first element
intersection = str(list(kwset.intersection(colorset))[0])
print(intersection)
driver.get('https://www.supremenewyork.com/' + intersection)
如果不能保证kwset.intersection(colorset)
始终至少返回一个元素,则需要执行一些错误处理,或者切换到另一种方式来“展开”集合,例如在集合的迭代器上调用默认的next