加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python:BeautifulSoup通过其类提取div标签之间的字符串

发布时间:2020-12-16 23:23:10 所属栏目:Python 来源:网络整理
导读:import urllib,urllib2from bs4 import BeautifulSoup,Commenturl='http://www.amazon.in/product-reviews/B00CE2LUKQ/ref=cm_cr_pr_top_link_1?ie=UTF8showViewpoints=0sortBy=bySubmissionDateDescending'content = urllib2.urlopen(url).read()soup = Bea
import urllib,urllib2
from bs4 import BeautifulSoup,Comment
url='http://www.amazon.in/product-reviews/B00CE2LUKQ/ref=cm_cr_pr_top_link_1?ie=UTF8&showViewpoints=0&sortBy=bySubmissionDateDescending'
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content,"html.parser")
rows =soup.find_all('div',attrs={"class" : "reviewText"})
print rows

此代码用于从网站提取评论.我只需要文本 – 但我用div标签得到它们.

关于如何单独提取文本,我需要帮助.我需要单独的文本 – 在div类标签之间.

解决方法

for row in soup.find_all('div',attrs={"class" : "reviewText"}):
    print row.text

要么:

[row.text for row in rows]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读