Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Beautiful soup-子标记的数量可变_Python_Beautifulsoup - Fatal编程技术网

Python Beautiful soup-子标记的数量可变

Python Beautiful soup-子标记的数量可变,python,beautifulsoup,Python,Beautifulsoup,我使用BeautifulSoup设置了以下函数来从页面提取数据。它工作得很好,只是在某些情况下,在传递给函数的next_tag属性下面有一个带有class元素的span标记,因此当我调用contents[0]时,我没有足够深入到我需要的真正内容 这是我正在抓取的一个页面,以及未能启动兴趣表达的元素 : 我尝试使用.string代替内容[0],但它似乎无法与findNext一起使用 我想出了一个惊人的方法让它工作,任何关于如何更有效地工作树的建议都将不胜感激。我添加了另一个函数来处理此数据类型,并

我使用BeautifulSoup设置了以下函数来从页面提取数据。它工作得很好,只是在某些情况下,在传递给函数的next_tag属性下面有一个带有class元素的span标记,因此当我调用contents[0]时,我没有足够深入到我需要的真正内容

这是我正在抓取的一个页面,以及未能启动兴趣表达的元素

:

我尝试使用.string代替内容[0],但它似乎无法与findNext一起使用


我想出了一个惊人的方法让它工作,任何关于如何更有效地工作树的建议都将不胜感激。我添加了另一个函数来处理此数据类型,并添加了一个if语句来处理len大于1的标记。我使用两个潜在标记调用函数,并根据标记的len计数向下过滤命令,如下所示:

def get_estimates (tender_soup, tag, text, next_tag, alt_tag):
if tender_soup.find(tag,text=text) == None:
    item_name = ''
    return item_name
else:
    if len(tender_soup.find(tag,text=text).findNext(next_tag).contents) == 0:
        item_name = ''
        return item_name
    else:
        if len(tender_soup.find(tag,text=text).findNext(next_tag)) > 1:
            item_name = tender_soup.find(tag,text=text).findNext(alt_tag).contents[0]
        else:
            item_name = tender_soup.find(tag,text=text).findNext(next_tag).contents[0]

    item_name = item_name.encode('utf-8')        
    item_name = item_name.strip()
    return item_name
return item_name

请提供HTML代码或链接以演示问题。谢谢:谢谢,请同时说明如何调用get_contents函数。eoi_start=get_contentstender_汤,标签,兴趣表达开始日期:,dd;和eoi_end=获取_内容的汤、标签、兴趣表达截止日期:、dd;
def get_estimates (tender_soup, tag, text, next_tag, alt_tag):
if tender_soup.find(tag,text=text) == None:
    item_name = ''
    return item_name
else:
    if len(tender_soup.find(tag,text=text).findNext(next_tag).contents) == 0:
        item_name = ''
        return item_name
    else:
        if len(tender_soup.find(tag,text=text).findNext(next_tag)) > 1:
            item_name = tender_soup.find(tag,text=text).findNext(alt_tag).contents[0]
        else:
            item_name = tender_soup.find(tag,text=text).findNext(next_tag).contents[0]

    item_name = item_name.encode('utf-8')        
    item_name = item_name.strip()
    return item_name
return item_name