python - 自动执行 Google Play 搜索列表中的项目

标签 python function web-scraping automation web-crawler

我正在开发一个 python 项目,我需要找出该公司拥有哪些应用程序。 例如,我有一个列表:

company_name = ['Airbnb', 'WeFi']

我想编写一个 python 函数/程序来执行以下操作:

1.让它自动在 Play 商店的列表中搜索项目

2.如果公司名称匹配,即使只匹配名字,例如“Airbnb”也会匹配“Airbnb,inc”

Airbnb Search Page circled

  • 然后它将点击进入页面并读取其类别 Airbnb Read category

  • 如果公司有多个应用,则会对所有应用执行相同的操作。

  • 公司的每个应用程序信息存储在tuple = {应用程序名称,类别}

  • 期望的最终结果将是元组列表

  • 例如:

    print(company_name[0])
    print(type(company_name[0]))
    

    结果:
    爱彼迎
    元组

    print(company_name[0][0])
    

    结果:
    [('爱彼迎','旅行')]

    这是许多知识的混合,我是Python的新手。因此,请给我一些指导,告诉我应该如何开始编写代码。

    我了解到 selenium 可以自动执行“加载更多”功能,但我不确定我到底可以使用什么包?

    最佳答案

    我编写了一个小演示,可以帮助您实现目标。我使用了 requests 和 Beautiful Soup。这并不完全是您想要的,但可以轻松调整。

    import requests
    import bs4
    
    company_name = "airbnb"
    def get_company(company_name):
        r = requests.get("https://play.google.com/store/search?q="+company_name)
        soup = bs4.BeautifulSoup(r.text, "html.parser")
        subtitles = soup.findAll("a", {'class':"subtitle"})
        dev_urls = []
        for title in subtitles:
            try:
                text = title.attrs["title"].lower()
            #Sometimes there is a subtitle without any text on GPlay
            #Catchs the error
            except KeyError:
                continue
            if company_name in text:
                url = "https://play.google.com" + title.attrs["href"]
                dev_urls.append(url)
        return dev_urls
    
    def get_company_apps_url(dev_url):
        r = requests.get(dev_url)
        soup = bs4.BeautifulSoup(r.text, "html.parser")
        titles = soup.findAll("a", {"class":"title"})
        return ["https://play.google.com"+title.attrs["href"] for title in titles]
    
    def get_app_category(app_url):
        r = requests.get(app_url)
        soup = bs4.BeautifulSoup(r.text, "html.parser")
        developer_name = soup.find("span", {"itemprop":"name"}).text
        app_name = soup.find("div", {"class":"id-app-title"}).text
        category = soup.find("span", {"itemprop":"genre"}).text
        return (developer_name, app_name, category)
    
    dev_urls = get_company("airbnb")
    apps_urls = get_company_apps_url(dev_urls[0])
    get_app_category(apps_urls[0])
    
    >>> get_company("airbnb")
    ['https://play.google.com/store/apps/developer?id=Airbnb,+Inc']
    >>> get_company_apps_url("https://play.google.com/store/apps/developer?id=Airbnb,+Inc")
    ['https://play.google.com/store/apps/details?id=com.airbnb.android']
    >>> get_app_category("https://play.google.com/store/apps/details?id=com.airbnb.android")
    ('Airbnb, Inc', 'Airbnb', 'Travel & Local')
    

    我与谷歌的脚本

    dev_urls = get_company("google")
    apps_urls = get_company_apps_url(dev_urls[0])
    for app in apps_urls:
        print(get_app_category(app))
    
    ('Google Inc.', 'Google Duo', 'Communication')
    ('Google Inc.', 'Google Translate', 'Tools')
    ('Google Inc.', 'Google Photos', 'Photography')
    ('Google Inc.', 'Google Earth', 'Travel & Local')
    ('Google Inc.', 'Google Play Games', 'Entertainment')
    ('Google Inc.', 'Google Calendar', 'Productivity')
    ('Google Inc.', 'YouTube', 'Media & Video')
    ('Google Inc.', 'Chrome Browser - Google', 'Communication')
    ('Google Inc.', 'Google Cast', 'Tools')
    ('Google Inc.', 'Google Sheets', 'Productivity')
    

    关于python - 自动执行 Google Play 搜索列表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39111251/

    相关文章:

    javascript - 日期中的自动点 - 在两个函数之间传递值还是其他函数?

    javascript - 如何在两个独立标签之间获取 HTML 元素

    python - TensorFlow 2.3 和 libcublas.so.10

    javascript - (hash) 在 javascript 中作为函数参数有什么意义吗?

    c - 我试图从函数返回一个指针?但我无法取回该指定地址的值(value)

    python - 抓取 : SSL: CERTIFICATE_VERIFY_FAILED error for http://en. wikipedia.org

    python - 使用 Selenium for Python 从 <table> 中迭代读取特定元素

    python - 当我在 PyCharm 中运行时记录 basicConfig 不创建日志文件?

    python - 过滤 HTML 文档中的所有内部文本

    python - Linux Fedora virtualenv 站点包位置不正确