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>
如果仅想要文档或标签的文本部分,则可以使用get_text()
方法。它以单个Unicode字符串为单位。
在您的情况下,smth类似:
soup.find_all()[0].get_text()
我认为没有必要进行循环。