2016-08-23 101 views
0

我正在研究一个python项目,我需要找出公司拥有的应用程序。 例如,我有一个列表:在列表中自动执行谷歌播放搜索项目

company_name = ['Airbnb', 'WeFi'] 

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

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

2。如果公司名称匹配,即使它只匹配的第一个名字,例如“制作的Airbnb”将匹配“的Airbnb,INC”

Airbnb Search Page circled

  • 然后,它会点击进入该页面并阅读其类别 Airbnb Read category

  • 如果公司有多个应用程序,它将为所有应用程序执行相同操作。

  • 公司的每个应用程序的信息是tuple = {app name, category}

  • 期望的最终结果店将元组

  • 如列表:

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

    结果:
    的Airbnb
    元组

    print(company_name[0][0]) 
    

    结果:
    [( '制作的Airbnb', '旅行')]

    这是一个混合的很多知识,我是一个新手,蟒蛇。所以请给我一些指导,我该如何开始编写代码。

    我学习硒可以自动执行“加载更多”功能,但我不确定我可以使用什么样的封装?

    +0

    脚本你有代码凑一个页面? –

    +0

    是的,我知道如何刮谷歌页面,但我做'自动化'部分有困难。我不知道如何在列表中自动执行搜索项并自动点击进入页面。 – KeepLearning

    +1

    添加你的代码,让你那么远 –

    回答

    0

    我写了一个小小的演示,可以帮助你实现你的目标。我使用了请求和美丽的汤。这不完全是你想要的,但它可以很容易地适应。

    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') 
    
    +0

    hi @ Peter234,这太好了!您提供了一个新的视角来处理我的问题。我会深入研究它!再次感谢!! – KeepLearning

    +0

    我只是想从下来的选民那里得到答案。我不应该张贴这么多的代码? – Peter234