我正试图通过在我的CRAIGLIST CLONE应用程序的新python项目中使用dJango框架美化组来获取嵌套标签中存储的信息。
import requests
from bs4 import BeautifulSoup
from django.shortcuts import render
from requests.compat import quote_plus
from . import models
BASE_CRAIGLIST_URL = 'https://ahmedabad.craigslist.org/search/?query={}'
search = request.POST.get('search')
models.Search.objects.create(search=search)
final_url = BASE_CRAIGLIST_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('p', {'class': 'result-info'})
final_postings = []
for post in post_listings:
post_title = post.find(class_='result-title').text
post_url = post.find('a').get('href')
post_price = post.find('span', class_='result-price')
final_postings.append((post_title, post_url, post_price))
stuff_for_front_end = {
'search' : search,
'final_postings': final_postings,
}
return render(request, template_name='myapp/new_search.html', context=stuff_for_front_end)
这是我想从哪里获得有关定价和所有信息的来源。我试过这个代码,得到了";无";在输出中。请参阅提交按钮下方的第3行
感谢您的合作,经过一些研究,我注意到价格不在class="result-info"的"p"中,而是在class="结果行"的"li"中,而且不是每个帖子都有价格。所以我尝试了一些技巧,其中一个奏效了。让我们看看。
def new_search(request):
search = request.POST.get('search')
models.Search.objects.create(search=search)
final_url = BASE_CRAIGLIST_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'})
final_postings = []
for post in post_listings:
post_title = post.find(class_='result-title').text
post_url = post.find('a').get('href')
if post.find('span', class_='result-price'):
post_price = post.find('span', class_='result-price').text
else:
post_price = 'N/A'
我还更改了界面。