,python,web-scraping,beautifulsoup"/>

首页 > 解决方案 > Python BeautifulSoup - 查找特定的

  • 问题描述

    我尝试从以下 HTML 中提取“li”:

    在此处输入图像描述

    我试过这样:

    soup = BeautifulSoup(html, 'html.parser')
    containers = soup.find('div', {'class': 'pagination-container'}).find('ul')`
    containers.li
    

    但我没有得到预期的结果。谁能帮我?

    标签: pythonweb-scrapingbeautifulsoup

    解决方案


    soup.find_all('li')[0]
    

    用于搜索页面上的所有 li,0 用于索引它。

    soup.find_all('span', class_='ad')
    

    搜索带有类广告的跨度

    我的跨度包含很多其他跨度的数据,我想摆脱这些数据,我知道我正在寻找这个特定的词

    Artist = soup.find_all('h1')[0]
    Artist = re.search('<h1>(.*)<span', str(Artist))
    Artist = Artist.group(0)
    Artist = str(Artist).replace('<h1>','')
    Artist = str(Artist).replace('<span','')
    

    我没有想出更好的东西,但它奏效了..我也很乐意推荐:)


    推荐阅读