提问人:ATSpiro 提问时间:11/16/2023 更新时间:11/16/2023 访问量:17
Django/Python Web Scraping - 没有找到目标标签来提取我需要的信息
Django/Python Web Scraping - the targeted tag isn't being found to pull the information I need
问:
我正在开发一个用于体育卡的 Django Web 应用程序。每张卡都有自己的页面,在顶部显示卡名称,在它下面应该显示该卡的当前最低价格。最低价格值是从 https://cardboard.market/ 中提取的。在我的 views.py 中,我想要一个获得最低价格的函数。这是我目前所处的位置:
views.py:
def get_lowest_card_price(card_name): base_url = “https://cardboard.market/” search_url = f“{base_url}search?q={card_name}”
try:
# Send a GET request to the website
response = requests.get(search_url)
response.raise_for_status() # Raise an exception for bad responses (4xx or 5xx)
# Parse the HTML content of the page
soup = BeautifulSoup(response.text, 'html.parser')
# Find the element with the specified class
target_div = soup.find('div', class_='bubble-element Text cmgaCs')
# Extract the text content
if target_div:
price_text = target_div.text.strip()
return price_text
else:
print(f"Could not find the specified div element for card '{card_name}'.")
except requests.RequestException as e:
print(f"Error during request: {e}")
return None
def ProSet1990_1(请求): # 用于演示目的的硬编码卡详细信息 card_name = “大卫·西曼” card_set = “1990 Pro Set”
# Use the web scraping bot to get the lowest card price
lowest_price = get_lowest_card_price(f'{card_set} {card_name}')
# Pass the data to the template
context = {
'card_name': card_name,
'lowest_price': lowest_price,
}
# Render the template with the data
return render(request, 'Application/ProSet1990_1.html', context)
当我运行服务器并转到卡的 html 页面时,我的 cmd 提示符显示“找不到卡'1990 Pro Set David Seaman'的指定 div 元素”。
我已经检查了网站上的标签是否匹配,以便提取卡的价格。如果有人有任何想法,请告诉我!谢谢!
答: 暂无答案
评论