在這里,我有一個 URL 串列,我試圖從所有這些 URL 中獲取“td”,但只能獲取最后一個 URL 的 HTML。
import numpy as np
import pandas as pd
from datetime import datetime
import pytz
import requests
import json
from bs4 import BeautifulSoup
url_list = ['https://www.coingecko.com/en/coins/ethereum/historical_data/usd?start_date=2021-08-06&end_date=2021-09-05#panel',
'https://www.coingecko.com/en/coins/cardano/historical_data/usd?start_date=2021-08-06&end_date=2021-09-05#panel',
'https://www.coingecko.com/en/coins/chainlink/historical_data/usd?start_date=2021-08-06&end_date=2021-09-05#panel']
for link in range(len(url_list)):
response = requests.get(url_list[link])
src = response.content
soup = BeautifulSoup(response.text , 'html.parser')
res1 = soup.find_all( "td", class_ = "text-center")
res1
任何人都可以幫助我如何獲取所有 URL 的資料嗎?
uj5u.com熱心網友回復:
您正在通過回圈的每次迭代覆寫您的湯變數。因此,與其保存每個 url 的所有結果,然后回圈遍歷這些結果,您只會得到最終結果。
- 在回圈之前創建一個變數來存盤每次迭代的結果
- 每次迭代將湯附加到該新變數
- 創建一個新回圈來與您存盤的資料進行互動
并且您可以通過以下方式訪問串列中的每個元素:
for url in url_list:
response = requests.get(url)
# rest of code
更容易閱讀
所以
# empty list to store all results
results = []
# your loop here
for u in url_list:
response = requests.get(url)
src = response.content
soup = BeautifulSoup(response.text , 'html.parser')
results.append(soup.find_all( "td", class_ = "text-center"))
# Accessing the data from the results
for result in results:
print(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/371478.html
上一篇:C 指標函式和new關鍵字
