我正在尝试从网站访问文章内容,使用带有以下代码的BeautifulGroup:
site= 'www.example.com'
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
content = soup.find_all('p')
content=str(content)
content对象包含“p”标记中页面的所有主文本,但是在输出中仍然存在其他标记,如下图所示。我要删除包含在匹配的标记对中的所有字符以及标记本身。只剩下文字了。
我试过以下方法,但似乎不起作用。
' '.join(item for item in content.split() if not (item.startswith('<') and item.endswith('>')))
什么是移除一个sting中的子字符串的最佳方法?以某种模式开始和结束的,如<;>
您需要使用strings generator:
使用正则表达式:
使用BeautifulSoup:(来自here的解决方案)
使用NLTK:
你可以用^{}
下面的示例来自docs:
相关问题 更多 >
编程相关推荐