【问题标题】:Not able to scrape content with BeautifulSoup无法使用 BeautifulSoup 抓取内容
【发布时间】:2014-11-16 11:59:18
【问题描述】:

我正在使用Python 和BeautifulSoup 进行网络抓取。

我必须刮掉这个页面。

http://www.starwoodhotels.com//sheraton/property/reviews/index.html?language=en_US&propertyID=115

从这个页面,我已经成功抓取了酒店的地址,但是我无法抓取用户评论部分

这是我的代码

hotel_link = "http://www.starwoodhotels.com//sheraton/property/reviews/index.html?language=en_US&propertyID=115"

hotel_page_html = requests.get(hotel_link,headers = header).text
hotel_page_soup = BeautifulSoup(hotel_page_html)

for hotel_address in hotel_page_soup.select("div#propertyAddressContainer ul#propertyAddress"):
  print("Address: "+hotel_address.select("li")[0].text)

print(hotel_page_soup.select("div.BVRRRatingNormalOutOf"))

如您所见,使用 CSS 选择器 div#propertyAddressContainer ul#propertyAddress,我得到了地址,但无法抓取 User Reviews 部分。

我在页面加载时检查了Console,但我没有看到任何用户评论是通过 AJAX 调用加载的。

那么我如何抓取评论部分?

【问题讨论】:

标签: python python-3.x beautifulsoup


【解决方案1】:

你为什么要搞得这么复杂?

就这样吧,

soup.find("span",{"itemprop":"aggregateRating"}).text.encode('ascii','ignore').replace('\n',' ')

Out[]:
Rated 3.4 out of 5by 625 reviewers.

这不是你需要的吗?

【讨论】:

  • 在您的情况下,它将是 hotel_page_soup.find 而不是 soup.find
  • 它没有用...我已经自己发布了答案...。非常感谢您的努力
【解决方案2】:

工作代码

rev = hotel_page_soup.find( "span",
                            { "itemprop": "aggregateRating" }
                            ).text.encode( 'ascii',
                                           'ignore'
                                           ).replace( '\n', ' ' )

for total_rating_score in rev.select( "span" ):
    print ( total_rating_score.string )

【讨论】:

  • 你的回答根本行不通。 rev 是一个字符串,它永远不会与 rev.select 一起使用,这太荒谬了。您提出问题,当有人回答您时,只需对一些甚至不正确的内容进行轻微修改并自己发布吗??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
相关资源
最近更新 更多