Scraped Span返回无Get_Text()Python美丽的汤

时间:2017-01-21 20:41:00

标签: python web-scraping beautifulsoup

我已经删除了汽车的链接,现在希望关注链接并抓取每辆车的一些数据,但我的代码返回一个空数组(如果我单独打印,则为无)。任何想法如何解决这个问题?

import bs4 as bs
import urllib

source = urllib.request.urlopen('http://www.25thstauto.com/inventory.aspx?cursort=asc&pagesize=500').read()
soup = bs.BeautifulSoup(source, 'lxml')

car = soup.select('a[id*=ctl00_cphBody_inv1_rptInventoryNew]')         
for a in car:
    source2 = urllib.request.urlopen('http://www.25thstauto.com/'+a.get('href')).read()
    price.append(soup.find('span', {'id': 'ctl00_cphBody_inv1_lblPrice'}))
    print(price)

1 个答案:

答案 0 :(得分:1)

import bs4 as bs
import urllib

source = urllib.request.urlopen('http://www.25thstauto.com/inventory.aspx?cursort=asc&pagesize=500').read()
soup = bs.BeautifulSoup(source, 'lxml')
price = []
car = soup.select('a[id*=ctl00_cphBody_inv1_rptInventoryNew]')         
for a in car:
    source2 = urllib.request.urlopen('http://www.25thstauto.com/'+a.get('href')).read()
    # make a new soup baesd on the link, do not use old soup
    soup2 = bs.BeautifulSoup(source2, 'lxml')
    price.append(soup2.find('span', {'id': 'ctl00_cphBody_inv1_lblPrice'}))
    print(price)

出:

[<span id="ctl00_cphBody_inv1_lblPrice">$2,995</span>]
[<span id="ctl00_cphBody_inv1_lblPrice">$2,995</span>, <span id="ctl00_cphBody_inv1_lblPrice">$2,995</span>]
[<span id="ctl00_cphBody_inv1_lblPrice">$2,995</span>, <span id="ctl00_cphBody_inv1_lblPrice">$2,995</span>, <span id="ctl00_cphBody_inv1_lblPrice">$2,995</span>]