在Python的BeatifulSoup中刮取itemprop =“ name”

Artemio Katrek
names = soup.find_all('meta', itemprop='name')
prices = soup.find_all('span', class_='price product-price')

for price, name in zip(prices, names):
    modelName = name
    modelPrice = price.text
    csv_writer.writerow([modelName, modelPrice])
print('Parsing prices: DONE')
csv_file.close()

这段代码之后,我将其导出到csv文件中,并得到如下所示的内容:

    "
"<meta content=""TEXT HERE"" itemprop=""name"">
</meta>","
          PRICE HERE

我想摆脱导出的代码,我只需要一个名称和价格。网站代码如下:

<a itemprop="name" class="product-name listgrid" href="https://websitename.com" title="Name of needed model to parse</a>
Linas Fx

如果只需要文档或标签的文本部分,则可以使用该get_text()方法。它以单个Unicode字符串的形式返回文档中或标签下的所有文本。

在您的情况下,类似的方法应该起作用:

soup.find_all()[0].get_text()

我认为没有必要循环。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章