我试图将这个对象的价格输入一个变量,并将其打印出来/放入CSV.
这是我试图解析的html部分:
<span class="price" data-js-product-price="">
<span>$429.00 USD</span>
</span>
这是我的python代码(抱歉,我对python有点陌生,我一直在努力解决这个问题,如果代码有点凌乱,那么很抱歉)
from bs4 import BeautifulSoup
url_to_scrape = "https://www.backfireboards.com/?gclid=CjwKCAjwjtOTBhAvEiwASG4bCGHPgmV4XjyqAIFrW0Lr0IiW0AvfTiC7sZ4E-HtM_qJ9k4ahAu2CHxoCH5YQAvD_BwE"
request_page = urlopen(url_to_scrape)
page_html = request_page.read()
request_page.close()
html_soup = BeautifulSoup(page_html, 'html.parser')
board_prices = html_soup.find_all('span', class_='price')
print("num of prices: " + str(len(board_prices)))
file_name = 'product.csv'
f = open(file_name,'w')
headers = 'Title, Price \n'
f.write(headers)
i = 1
for price in board_prices:
currPrice = price.span.text
print(i)
i = i + 1
print(price)
print(currPrice)
f.close()
以下是我得到的错误:
Traceback (most recent call last):
File "/Users/isaiah/PycharmProjects/Web_scrape/main.py", line 26, in <module>
currPrice = price.span.text
AttributeError: 'NoneType' object has no attribute 'text'
我知道这不是文本对象,但当我打印它时,没有.短信中写道:
1
<span class="price" data-js-popup-cart-subtotal=""></span>
None
2
<span class="price" data-js-product-price=""><span></span></span>
<span></span>
3
<span class="price" data-js-product-price=""><span></span></span>
<span></span>
4
<span class="price"><span></span></span>
<span></span>
5
<span class="price" data-js-product-price=""><span></span></span>
<span></span>
6
<span class="price" data-js-product-price=""><span></span></span>
<span></span>
我不知道为什么429美元会消失在物品中,我对网络垃圾之类的东西有点陌生.有什么很简单的事我也不知道吗?
另外,从我所能告诉你的,这个网站在这个页面上列出了8个价格,而board_prices对象的大小只有6?有人能给我解释一下吗?