beauthulsoup从htmlli标签中提取特定项

2024-05-08 22:11:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用BeautifulSoup从htmlli标记中提取特定的项。我在程序的这一部分发现了一个错误,它停止了程序:

 product_url = 'https://example.com/product'
 page = sess.get(product_url)
 page = BeautifulSoup(page.text)
 list = page.select('li.items')
 list_items = [
            {
              "menu_name": menu.find('h3').text,
              "menu_type": menu.find('strong').text.strip(),
              "menu_level": menu.select('.list_item')[0].text.strip() if menu.select('.list_item') else '' ,
              "menu_url": BASE_URL + menu.select('a.title')[0]['href'],
            }
            for menu in list]
            #Any code after this line not worked

另外,如何在对象内部打印字符串以进行调试?在


Tags: text标记程序urlpageitemsfindproduct