不确定为什么beautifulsoup代码不会抓取网站

时间:2019-09-07 19:27:30

标签: python parsing web-scraping beautifulsoup kodi

我已经使用BS了很多,但是我不确定为什么不会如此,因为我为Kodi制作的其他插件效果很好。有人可以看看标记之间的代码,也许找到我所缺少的地方吗?

addon / python不会抛出任何错误,它只是提供一个空的GUI屏幕。如果标题或图像抓取效果很好并且链接不正确,则它将显示标题/图像,但单击该链接后将不起作用。因此,显然是标题/图像部分。我什至尝试对图像部分进行散列处理,因此它只查找链接和标题,但什么也没有。

链接被抓取:https://store.counterpunch.org/feed/podcast/

def get_soup1(url1):
    page = requests.get(url1)
    soup1 = BeautifulSoup(page.text, 'html.parser')
    print("type: ", type(soup1))
    return soup1
get_soup1("https://store.counterpunch.org/feed/podcast/")

def get_playable_podcast1(soup1):
    subjects = []
    for content in soup1.find_all('item', limit=9):
        try:        
            link = content.find('enclosure')
            link = link.get('url')
            print("\n\nLink: ", link)
            title = content.find('title')
            title = title.get_text()
        except AttributeError:
            continue
        item = {
                'url': link,
                'title': title,
                'thumbnail': "https://is2-ssl.mzstatic.com/image/thumb/Podcasts71/v4/71/55/88/71558834-c449-9ac3-e327-cad002e305b4/mza_4409042347411679857.jpg/600x600bb.jpg",
        }
        subjects.append(item)
    return subjects
def compile_playable_podcast1(playable_podcast1):
    items = []
    for podcast in playable_podcast1:
        items.append({
            'label': podcast['title'],
            'thumbnail': podcast['thumbnail'],
            'path': podcast['url'],
            'is_playable': True,
    })
    return items

1 个答案:

答案 0 :(得分:0)

您需要一个用户代理

def get_soup1(url1):
    page = requests.get(url1, headers = {'User-Agent':'Mozilla/5.0'})
    soup1 = BeautifulSoup(page.text, 'html.parser')
    print("type: ", type(soup1))
    return soup1