Scraped Span返回None Get_Text()Python Beautiful Soup

2024-10-01 15:48:12 发布

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

我已经抓取了汽车的链接,现在希望跟踪这些链接并获取关于每辆车的一些数据,但我的代码返回一个空数组(如果我单独打印,则没有)。有什么办法解决这个问题吗?在

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)

Tags: importcomidhttpsourcereadbs链接
1条回答
网友
1楼 · 发布于 2024-10-01 15:48:12
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)

输出:

^{pr2}$

相关问题 更多 >

    热门问题