len()函数没有给出正确的字符编号

时间:2018-04-25 17:26:44

标签: python-3.x beautifulsoup

我试图找出一个字符串中的字符数,但由于一些奇怪的原因,len()只给了我1。 这是我输出的一个例子

WearWorks is a haptics design company that develops products and 
experiences that communicate information through touch. Our first product, 
Wayband, is a wearable tactile navigation device for the blind and visually 
impaired.
True
1

这是我的代码

import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

url="https://www.wear.works/"
response=requests.get(url)
html=response.content
soup=BeautifulSoup(html,'html.parser')

#reference https://stackoverflow.com/questions/328356/extracting-text-from-html-file-using-python
# getting rid of the script sytle in html
for script in soup(["script", "style"]):
    (script.extract())    # rip it out
    # print(script)

# get text
# grabbing the first chunk of text
text = soup.get_text()[0]
print(isinstance(text, str))
print(len(text))

print(text)

1 个答案:

答案 0 :(得分:1)

问题是text = soup.get_text()[0]将其转换为text = soup.get_text()看看。你正在切割一个字符串来获得第一个字符。