Использование Beautifulsoup для анализа более одного сайта - PullRequest
0 голосов
/ 05 июля 2019

Благодаря замечательным людям на этом форуме, я успешно создал рабочий скрипт для извлечения подкаста с сайта. Следующий код работает нормально, мне просто нужно извлечь изображение (миниатюру) из следующего бита кода, а не по ссылке в команде soup.find_all:

def get_playable_podcast(soup):
    """
    @param: parsed html page            
    """
    subjects = []

    for content in soup.find_all('item'):

        try:        
            link = content.find('enclosure')
            link = link.get('url')
            print "\n\nLink: ", link

            title = content.find('title')
            title = title.get_text()

            desc = content.find('itunes:subtitle')
            desc = desc.get_text()

            thumbnail = content.find('itunes:image')
            thumbnail = thumbnail.get('href')

        except AttributeError:
            continue

    item = {
            'url': link,
            'title': title,
            'desc': desc,
            'thumbnail': thumbnail
    }

    subjects.append(item) 

return subjects

def compile_playable_podcast(playable_podcast):
"""
@para: list containing dict of key/values pairs for playable podcasts
"""
items = []

for podcast in playable_podcast:
    items.append({
        'label': podcast['title'],
        'path': podcast['url'],
        'info': podcast['desc'],
        'is_playable': True,})

return items

Следующий код, который у меня есть, буквально загружает изображение, мне нужно в основном объединить его в приведенный выше код и создать ссылки на изображение, а не загружать его, чтобы в конечном итоге его можно было включить в раздел 'subjects.append'. Любая помощь будет принята с благодарностью:

resp = requests.get("https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=1000000&page=1").json()
df = pd.DataFrame(resp['posts'], columns=['image'])
df['image'] = df['image'].apply(pd.Series)['large'].replace({'"': '\'','""': '\'','"""': '\'' }, regex=True)
Regex_Pattern = r"([^\/]+$)"

        for index, row in df.iterrows():
            match = re.findall(Regex_Pattern, row['image'])
            myfilename = ''.join(match)
            print(row['image'])
            print(myfilename)
            urllib.urlretrieve(row['image'], myfilename)
...