使用BeautifulSoup在主网页的子页面中搜索

时间:2018-08-13 11:37:00

标签: python web-scraping beautifulsoup

我正在尝试用div搜索class = 'class',但是我需要在主页以及子(或子)页面中找到所有匹配项。我该如何使用BeautifulSoup或其他任何方式?

我在此搜索中找到了最接近的答案 Search the frequency of words in the sub pages of a webpage using Python 但此方法仅检索部分结果,感兴趣的页面还有更多子页面。还有另一种方法吗?

到目前为止,我的代码:

from bs4 import BeautifulSoup
soup = BeautifulSoup(page.content, 'html.parser')
subpages = []
for anchor in soup.find_all('a', href=True):
    string = 'https://www.mainpage.nl/'+str(anchor['href'])
    subpages.append(string)
for subpage in subpages:
    try:
        soup_sub = BeautifulSoup(requests.get(subpage).content, 'html.parser')
        promotie = soup_sub.find_all('strong', class_='c-action-banner__subtitle')
        if len(promotie) > 0:
            print(promotie)
    except Exception:
        pass

谢谢!

0 个答案:

没有答案
相关问题