首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优美汤解析错误

优美汤解析错误
EN

Stack Overflow用户
提问于 2016-05-07 13:30:48
回答 1查看 1.3K关注 0票数 1

我正在尝试提取一些关于Google的应用程序的信息,而BeautifulSoup似乎不起作用。

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

我的代码:

代码语言:javascript
复制
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

我决定全身心投入,也没有发挥作用:

代码语言:javascript
复制
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

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-07 13:36:50

您需要通过提供用户代理头来假装自己是真正的浏览器。

代码语言:javascript
复制
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)

打印标题和当前评等:

代码语言:javascript
复制
Weird Facts
Rated 4.3 stars out of five stars

要获得附加信息字段值,可以使用以下泛型函数:

代码语言:javascript
复制
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)

那么,如果你这样做:

代码语言:javascript
复制
print(get_info(soup, "Size"))
print(get_info(soup, "Developer"))

你会看到印刷品:

代码语言:javascript
复制
1.4M
Email email@here.com
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37089174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档