我试图计算一堆HTML文件中的图片数量,这些文件我已经读入Pandas DataFrame(在_text
列下)。我的尝试在这里:
text_img_count = [len(BeautifulSoup(x, "lxml").find_all('img')) for x in df['_text']]
然而,我收到此错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-1d0e442bf1d1> in <module>()
----> 1 text_img_count = [len(BeautifulSoup(x, "lxml").find_all('img')) for x in df['_text']]
2 text_vid_count = [len(BeautifulSoup(x, "lxml").find_all('video')) for x in df['_text']]
3 text_link_count = [len(BeautifulSoup(x, "lxml").find_all('a')) for x in result_df['_text']]
4 df['_text_img_count'] = text_img_count
5 df['_text_vid_count'] = text_vid_count
/Users/anaconda/lib/python2.7/site-packages/bs4/__init__.pyc in __init__(self, markup, features, builder, parse_only, from_encoding, exclude_encodings, **kwargs)
190 if hasattr(markup, 'read'): # It's a file-type object.
191 markup = markup.read()
--> 192 elif len(markup) <= 256 and (
193 (isinstance(markup, bytes) and not b'<' in markup)
194 or (isinstance(markup, unicode) and not u'<' in markup)
TypeError: object of type 'float' has no len()
然后,我检查了我的_text
列的类型,它是一个浮动...
我使用df = pd.read_csv('full_data.csv', index_col=0)
来读取我的数据。我对它为什么以浮动方式读取我的HTML标记感到有些困惑...
知道如何解决?
谢谢!