我正在使用BeautifulSoup软件包来抓取一个网站。
我使用以下代码
将我们要查找的内容解压缩到名为l_results的变量中l_results = soup.find_all('div',attrs={"class":"gitb-section-content"})
这将返回以下数据:
[<div class="gitb-section-content" data-section_name="valuable_features">\n<ul>\n<li>Passcode enforcement on devices containing corporate email or data</li>\n<li>The notification of new devices accessing corporate email and VPN connectivity</li>\n<li>Deploying needed applications to device groups</li>\n</ul>\n</div>,
<div class="gitb-section-content" data-section_name="improvements_to_organization">\n<p>The product has given us complete control of devices allowed to receive company data. It is important that only salaried employees receive corporate email on mobile devices. Checking and responding to corporate email outside of normal scheduled shifts by hourly employees, can and should be time paid.</p>\n</div>,
<div class="gitb-section-content" data-section_name="room_for_improvement">\n<p>I would like to see one-click app distribution to a single device or user. Perhaps I need further instruction in this area if it is supposed to function in this way currently. I would also like the ability to add a nagging message to any user that falls out of compliance.</p>\n</div>,
<div class="gitb-section-content" data-section_name="use_of_solution">\n<p>I've used it for three years.</p>\n</div>,
<div class="gitb-section-content" data-section_name="stability_issues">\n<p>It does seem that the more devices we added, the slower the management console operates.</p>\n</div>,
<div class="gitb-section-content" data-section_name="other_advice">\n<p>We are very pleased with the Maas360 product and plan to continue use as our company grows.</p>\n</div>]
现在我正在尝试从&#39; p&#39;中提取文字。和&#39; li&#39;标签,因为有些评论可能包含段落文本和列表项目(原来并不知道li)。
通过使用以下内容,我可以获得那些不包含列表项的结果:
for x in l_results:
review_text += '\n' + ''.join(x.find('p').text)
当代码遇到li的评论时,我得到以下结果:
File "<ipython-input-63-d24fd128d779>", line 2, in <module>
review_text += '\n' + ''.join(x.find('p').text)
AttributeError: 'NoneType' object has no attribute 'text'
答案 0 :(得分:1)
尝试仅在段落文本存在时获取
for x in l_results:
review_text += '\n'
_p = x.find('p')
if _p:
review_text += ''.join(_p.text)