【发布时间】:2019-05-29 09:14:12
【问题描述】:
我使用 python 做了一个网络爬虫,一切都运行良好,直到它到达这部分代码:
# Use BeautifulSoup modules to format web page as text that can
# be parsed and indexed
#
soup = bs4.BeautifulSoup(response, "html.parser")
tok = "".join(soup.findAll("p", text=re.compile(".")))
# pass the text extracted from the web page to the parsetoken routine for indexing
parsetoken(db, tok)
documents += 1
我得到的错误是TypeError: sequence item 0: expected str instance,在代码中的 tok 行周围找到了标记。
我认为我的语法可能是问题,但我不确定。我该如何解决这个问题?
【问题讨论】:
-
您传递给
''.join的不是字符串的可迭代,它必须是。soup.findall返回一些我只能假设的自定义对象的序列 -
你可能需要
tok = "".join([x.text for x in soup.findAll("p", text=re.compile(".")))
标签: python python-3.x beautifulsoup