错误在一个带有美味汤的代码中,.gettext方法

2024-03-29 11:10:30 发布

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

我遇到了美丽的汤模块错误。 我在一个网站上选择了一些数据,我可以得到带有标签的数据,没有任何问题,但是每当我尝试它失败时,它就会把变量当作一个列表,尽管我从bs4中了解到它不应该

代码如下:

   novsajt = requests.get(prpage, headers={'User-Agent': 'Mozilla/5.0'})
   novasupa = soup(novsajt.text, "html.parser")
   nesto = novasupa.find_all('p', class_='name product-title')

   for b_tag in nesto:
       for a_tag in b_tag:
            product_linkovi = a_tag['href']
            source11 = requests.get(product_linkovi, headers={'User-Agent': 'Mozilla5.0'})
            soup11 = soup(str(source11), "html.parser")
            productName = soup11.select("main#main h1").get_text() 
            listaproizvoda.append("productName" + str(productName)+ ";")
            synonims = soup11.select("main#main div.product-short-description > table > tbody > tr:nth-of-type(1) > td:nth-of-type(2)").get_text()
            listaproizvoda.append("synonims" +  str(synonims)+ ";")
            Application = soup11.select("main#main div.product-short-description > table > tbody > tr:nth-of-type(2) > td:nth-of-type(2)").get_text()
            listaproizvoda.append("Application" + str(Application)+ ";")

那么,您建议如何提取没有标记的文本?我犯了什么样的错误


Tags: oftextgetmaintagtypeproductselect