Python 没有得到价格,我得到了';非类型';对象没有属性';文本';
我正在craigslist网站上尝试拉屎。我想能够检索产品标题,链接和奖品,但我得到了这个错误Python 没有得到价格,我得到了';非类型';对象没有属性';文本';,python,beautifulsoup,Python,Beautifulsoup,我正在craigslist网站上尝试拉屎。我想能够检索产品标题,链接和奖品,但我得到了这个错误 post_price = post_listings[0].find(class_='result-price').text AttributeError: 'NoneType' object has no attribute 'text' 我试图删除.text,但价格结果是“无”,而不是显示实际价格 import requests from bs4 import BeautifulSoup fro
post_price = post_listings[0].find(class_='result-price').text
AttributeError: 'NoneType' object has no attribute 'text'
我试图删除.text
,但价格结果是“无”
,而不是显示实际价格
import requests
from bs4 import BeautifulSoup
from django.shortcuts import render
from requests.compat import quote_plus
from . import models
BASE_CRAIGSLIST_URL = 'https://losangeles.craigslist.org/d/services/search/bbb?query={}'
# Create your views here.
def home(request):
return render(request, 'base.html')
def new_search(request):
search = request.POST.get('search')
models.Search.objects.create(search=search)
final_url = BASE_CRAIGSLIST_URL.format(quote_plus(search))
print(final_url)
response = requests.get(final_url)
data = response.text
soup = BeautifulSoup(data, features='html.parser')
post_listings = soup.find_all('li', {'class': 'result-row'})
post_title = post_listings[0].find(class_='result-title').text
post_url = post_listings[0].find('a').get('href')
post_price = post_listings[0].find(class_='result-price').text
print(post_title)
print(post_url)
print(post_price)
#print(data)
stuff_for_frontend = {
'search': search,
}
return render(request, 'my_app/new_search.html', stuff_for
您的问题是,您的基本url用于服务,而不是用于销售的产品。服务没有列出价格 如果将url更改为以下内容,则脚本将搜索产品而不是服务,并应查找价格。
BASE\u CRAIGSLIST\u URL='1〕https://losangeles.craigslist.org/search/sss?query={}
这意味着结果价格
不存在。这意味着它返回None
,这是Python非常基本的一部分