簡體   English   中英

如何從 Django 中的第 3 方網站獲取我的網站的數據

[英]How to fetch data for my website from 3rd party website in Django

我想學習,如何從任何網站(具有特定的給定鏈接)獲取任何類型的 integer 值 假設如果我在谷歌中搜索today's temperature ,它會顯示如下:

在此處輸入圖像描述

我更喜歡使用 Django。 如何從此頁面獲取溫度值? 我的意思是views.py應該是什么樣子? 我對這種類型的數據獲取完全陌生,對此了解為零,如果您願意,請向我推薦一些教程鏈接或博客。

我建議改用 API 操作,因為它們是為與應用程序交互而設計的。 如果您嘗試以編程方式訪問搜索結果,Google 可能會產生懷疑,並要求您填寫驗證碼以證明您是人類。

除此之外,這是你將如何做到的:

  1. 使用requests獲取網站的 HTML 內容
  2. 使用bs4 解析 HTML
  3. 從 HTML 中提取所需數據

例如:

from bs4 import BeautifulSoup
import requests

url = 'https://www.accuweather.com/en/no/oslo/254946/current-weather/254946'
r = requests.get(url) # get the page content using requests

soup = BeautifulSoup(r, 'html.parser') # parse the page HTML

current_temp = soup.select('.display-temp').get_text() # locate the element of interest, and get the element's text content

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM