【问题标题】:Beautifulsoup parsing errorBeautifulsoup 解析错误
【发布时间】:2016-09-02 12:26:35
【问题描述】:

我正在尝试提取有关 Google Play 上某个应用的一些信息,但 BeautifulSoup 似乎不起作用。

链接是这样的(比如说): https://play.google.com/store/apps/details?id=com.cimaxapp.weirdfacts

我的代码:

url = "https://play.google.com/store/apps/details?id=com.cimaxapp.weirdfacts"
r = requests.get(url)
html = r.content
soup = BeautifulSoup(html)
l = soup.find_all("div", { "class" : "document-subtitles"})
print len(l)
0 #How is this 0?! There is clearly a div with that class

我决定全力以赴,也没有用:

i = soup.select('html body.no-focus-outline.sidebar-visible.user-has-no-subscription div#wrapper.wrapper.wrapper-with-footer div#body-content.body-content div.outer-container div.inner-container div.main-content div div.details-wrapper.apps.square-cover.id-track-partial-impression.id-deep-link-item div.details-info div.info-container div.info-box-top')
print i

我做错了什么?

【问题讨论】:

  • 您的标题显示错误,但您的问题正文没有显示错误。你得到什么错误?
  • 最大的可能性之一是您要查找的内容是使用 javascript 加载的。
  • 我没有得到我想要的div 元素,即使使用正确的搜索查询。注意我的 ResultSet 没有结果。什么给了?
  • 你检查过汤的内容吗?并非所有网站都喜欢被抓取(至少在不伪造浏览器的情况下不会)

标签: python web-scraping beautifulsoup html-parsing


【解决方案1】:

您需要通过提供 User-Agent 标头来伪装成真正的浏览器

import requests
from bs4 import BeautifulSoup

url = "https://play.google.com/store/apps/details?id=com.cimaxapp.weirdfacts"
r = requests.get(url, headers={
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36"
})
html = r.content
soup = BeautifulSoup(html, "html.parser")

title = soup.find(class_="id-app-title").get_text()
rating = soup.select_one(".document-subtitle .star-rating-non-editable-container")["aria-label"].strip()

print(title)
print(rating)

打印标题和当前评分:

Weird Facts
Rated 4.3 stars out of five stars

要获取附加信息字段值,可以使用以下通用函数:

def get_info(soup, text):
    return soup.find("div", class_="title", text=lambda t: t and t.strip() == text).\
        find_next_sibling("div", class_="content").get_text(strip=True)

那么,如果你这样做:

print(get_info(soup, "Size"))
print(get_info(soup, "Developer"))

你会看到打印出来的:

1.4M
Email email@here.com

【讨论】:

  • 对我不起作用。也许他们已经意识到我是一个刮板。疯了吧。讨厌被大公司摆布。我认为谷歌是一家好公司。 别作恶。呸!
  • @DarshanChaudhary 很有趣,尝试使用不同的 IP 地址,看看是否是您所怀疑的。顺便问一下,您是否检查过 Google Play API 是否适合您?谢谢。
  • 仍然被阻止。你能得到应用程序图标图片、大小、安装和开发者电子邮件 ID 的 CSS 选择器吗?我欠你一个。
  • @DarshanChaudhary 好的,更新了一个示例。希望对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 2023-03-16
  • 2015-09-08
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
相关资源
最近更新 更多