Получить href ссылки и текст в цикле Python - PullRequest
1 голос
/ 26 апреля 2019

Мне нужно собрать информацию из Apple Store, у меня есть хэш-карта hashmap_genre_link с жанром и URL ({'Games': 'https://itunes.apple.com/us/genre/ios-games/id6014?mt=8'; ...}), которые я хочу создать для каждый ключ - это другая хэш-карта с iOS-приложениями (текст) и URL-адресом приложения в качестве значения: games_apps: {'Pokemon Go', 'https://itunes.apple.com/us/app/pokémon-go/id1094591345?mt=8': ...}.

Вот мой код:

from bs4 import BeautifulSoup

from requests import get

links = []
ios_categories_links=[]
hashmap_genre_link ={}
url = "https://itunes.apple.com/US/genre/ios/id36"
response = get(url)
html_soup = BeautifulSoup(response.text,"html.parser")
categories_class = html_soup.find_all('div',class_="grid3-column")
# cat = categories_class.text
href = html_soup.find_all('a', href=True)
for j in href:
    # print(j['href'])
    links.append(j['href'])

#
# Hasmap initialisation : hashmap_genre_link = {"games" : "https://link_for_games_page"; etc...}
for i in links:
    if "https://itunes.apple.com/us/genre/ios" in i:
        genre = i.split("/")[5][4:] #We get the genre, without 'ios-'
        hashmap_genre_link[genre] = i
        ios_categories_links.append(i)

#print(hashmap_genre_link)


for the_key, the_value in hashmap_genre_link.items():
    #print(the_key, 'corresponds to', the_value)
    print("=======================")
    print(the_key)
    response_genre_link = get(the_value)
    html_soup_genre_link = BeautifulSoup(response_genre_link.text,"html.parser")
    genre_popular_apps_class = html_soup_genre_link.find_all('div',class_="grid3-column")
    for x in genre_popular_apps_class:
        print(x['href'])

Вот часть вывода:

=======================
games-family
<div class="grid3-column" id="selectedcontent">
<div class="column first">
<ul>
<li><a href="https://itunes.apple.com/us/app/trivia-crack/id651510680?mt=8">Trivia Crack</a> </li>
<li><a href="https://itunes.apple.com/us/app/minion-rush/id596402997?mt=8">Minion Rush</a> </li>
<li><a href="https://itunes.apple.com/us/app/draw-something-classic/id488628250?mt=8">Draw Something Classic</a> </li>

Как я могу получить тег href в значении. (Для текста, который я знаю, я могу использовать .text

1 Ответ

1 голос
/ 26 апреля 2019

У вас есть правильная идея с ['href'], чтобы получить значения этих атрибутов. Тем не менее, вы должны изолировать их. Ваши элементы x содержат все эти ссылки с тегами <a>. Таким образом, вам нужно будет сделать еще x.find_all('a'), затем пройтись по ним и распечатать каждый атрибут href для каждого из этих <a> тегов.

Итак, что я добавил:

for x in genre_popular_apps_class:
        alpha = x.find_all('a')   
        for beta in alpha:
            print (beta['href'])

Полный код:

from bs4 import BeautifulSoup

from requests import get

links = []
ios_categories_links=[]
hashmap_genre_link ={}
url = "https://itunes.apple.com/US/genre/ios/id36"
response = get(url)
html_soup = BeautifulSoup(response.text,"html.parser")
categories_class = html_soup.find_all('div',class_="grid3-column")
# cat = categories_class.text
href = html_soup.find_all('a', href=True)
for j in href:
    # print(j['href'])
    links.append(j['href'])

#
# Hasmap initialisation : hashmap_genre_link = {"games" : "https://link_for_games_page"; etc...}
for i in links:
    if "https://itunes.apple.com/us/genre/ios" in i:
        genre = i.split("/")[5][4:] #We get the genre, without 'ios-'
        hashmap_genre_link[genre] = i
        ios_categories_links.append(i)

#print(hashmap_genre_link)

results_dict = {}
for the_key, the_value in hashmap_genre_link.items():
    #print(the_key, 'corresponds to', the_value)
    print("=======================")
    print(the_key)
    response_genre_link = get(the_value)
    html_soup_genre_link = BeautifulSoup(response_genre_link.text,"html.parser")
    genre_popular_apps_class = html_soup_genre_link.find_all('div',class_="grid3-column")
    for x in genre_popular_apps_class:
        alpha = x.find_all('a')
        links = [ beta['href'] for beta in alpha ]

    results_dict[the_key] = links

Выход:

....
=======================
games-racing
https://itunes.apple.com/us/app/bike-race-free-style-games/id510461758?mt=8
https://itunes.apple.com/us/app/hill-climb-racing/id564540143?mt=8
https://itunes.apple.com/us/app/csr-racing/id469369175?mt=8
https://itunes.apple.com/us/app/real-racing-3/id556164008?mt=8
https://itunes.apple.com/us/app/asphalt-8-airborne/id610391947?mt=8
https://itunes.apple.com/us/app/csr-racing-2/id887947640?mt=8
https://itunes.apple.com/us/app/smashy-road-wanted/id1020119327?mt=8
https://itunes.apple.com/us/app/happy-wheels/id648668184?mt=8
https://itunes.apple.com/us/app/angry-birds-go/id642821482?mt=8
https://itunes.apple.com/us/app/need-for-speed-no-limits/id883393043?mt=8
...
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...