Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用replace()替换列表中的单词?_Python_Html_Web Scraping_Replace - Fatal编程技术网

Python 如何使用replace()替换列表中的单词?

Python 如何使用replace()替换列表中的单词?,python,html,web-scraping,replace,Python,Html,Web Scraping,Replace,我正试图从我的网页抓取中创建一个列表,但任何时候都有一个空格显示为“%20”。例如,“好日子”将变成“好日子”。我试着用replace,但它说不能用在列表上。有什么想法吗?谢谢 number = 1 while number < 10: containers = page_soup.findAll("span", {"class":"css-133coio etbu2a32"}) container = containers[number] con_str

我正试图从我的网页抓取中创建一个列表,但任何时候都有一个空格显示为“%20”。例如,“好日子”将变成“好日子”。我试着用replace,但它说不能用在列表上。有什么想法吗?谢谢

number = 1

while number < 10:

    containers = page_soup.findAll("span", {"class":"css-133coio etbu2a32"})

    container = containers[number]

    con_str = str(container)

    split_str = con_str.split('/browse/')

    split_wrd = split_str[1]

    needWord = split_wrd.split('"')

    final_Word = needWord.replace("%20", " ")

    print(final_Word[0])

    number += 1
代码有点贫民区,但它可以工作。

findAll属性返回列表,所以您应该替换列表中的每个元素

containers = page_soup.findAll("span", {"class":"css-133coio etbu2a32"})

for index, word in enumerate(containers) :
  word = word.replace('%20'," ")
  containers[index] = word
print(containers)

你能分享更多你的程序吗,包括HTML的例子或者至少是容器的内容?除了将数据转换为字符串并像那样拆分它之外,您没有其他选择吗?此外,变量和函数名应遵循小写字母带下划线的样式。您似乎混合了多种命名约定。是的,我首先找到了needWord的[0],然后替换了它。