使用BeautifulSoup进行解析时出现的错误消息:TypeError:强制转换为Unicode:需要string或buffer,但找到了NoneType

2024-09-27 00:20:12 发布

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

所以我试图从Amazon页面中获取数据,当我试图解析卖家所在的位置时,遇到了一个错误。我的代码是:

#getting the html
request = urllib2.Request('http://www.amazon.com/gp/offer-listing/0393934241/')
opener = urllib2.build_opener()
#hiding that I'm a webscraper
request.add_header('User-Agent', 'Mozilla/5 (Solaris 10) Gecko')
#opening it up, putting into soup form
html = opener.open(request).read()
soup = BeautifulSoup(html, "html5lib")

#parsing for the seller info
sellers = soup.findAll('div', {'class' : 'a-row a-spacing-medium olpOffer'})
for eachseller in sellers:
    #parsing for price
    price = eachseller.find('span', {'class' : 'a-size-large a-color-price olpOfferPrice a-text-bold'})
    #parsing for shipping costs
    shippingprice = eachseller.find('span'
    , {'class' : 'olpShippingPrice'})
    #parsing for condition
    condition = eachseller.find('span', {'class' : 'a-size-medium'})
    #parsing for seller name
    sellername = eachseller.find('b')
     #parsing for seller location
    location = eachseller.find('div', {'class' : 'olpAvailability'})

    #printing it all out
    print "price, " + price.string + ", shipping price, " + shippingprice.string + ", condition," + condition.string + ", seller name, " + sellername.string + ", location, " + location.string

我收到错误消息,与末尾的“print”命令有关: TypeError: coercing to Unicode: need string or buffer, NoneType found

我知道它来自这一行-location = eachseller.find('div', {'class' : 'olpAvailability'})-因为没有那行代码可以正常工作,我知道我得到了NoneType,因为这行没有找到任何东西。以下是我要解析的部分中的html:

^{pr2}$

我不明白“位置”这行代码有什么问题,也不知道为什么它不能提取我想要的数据。在

编辑:我想出来了,但我不知道为什么。如果将打印命令更改为 打印位置。查找(文本=真) 它输出我想要的位置。希望有一天这会对某人有所帮助。在


Tags: 代码forstringrequesthtmllocationfindopener
1条回答
网友
1楼 · 发布于 2024-09-27 00:20:12

好像你找错了类名

<div class="a-column a-span3 olpDeliveryColumn" role="gridcell">
<p class="a-spacing-mini olpAvailability">
<ul class="a-unordered-list a-vertical olpFastTrack">
<li><span class="a-list-item">
            Ships from WI, United States.
        </span></li>
<li><span class="a-list-item">
<a href="/gp/aag/details?ie=UTF8&amp;asin=0393934241&amp;seller=A263RIO308P3G8&amp;sshmPath=shipping-rates#aag_shipping">Shipping rates</a>
                   and <a href="/gp/aag/details?ie=UTF8&amp;asin=0393934241&amp;seller=A263RIO308P3G8&amp;sshmPath=returns#aag_returns">return policy</a>.
        </span></li>
</ul>
</p>
</div>

更改代码中的以下行:

^{pr2}$

相关问题 更多 >

    热门问题