我正在尝试刮取coinmarketcap.com只是为了获得某个货币价格的更新,也只是为了学习如何通过网络刮取。我仍然是一个初学者,无法弄清楚哪里出了问题,因为每当我尝试运行它时,它只会告诉我没有错误。虽然我知道那条线确实存在。任何帮助表示赞赏!
import requests
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/currencies/electroneum/'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
price = soup.find('data-currency-price data-usd=')
print (price)
答案 0 :(得分:1)
如果您打算进行很多此操作,请考虑使用official API进行一次通话并获得所有价格。然后提取您想要的。以下是我修改后的站点,以显示所需的电子um值。 API指南还显示了如何检索one at a time,尽管这需要比基本方案更高的计划。
Safari
您始终可以部署循环并对照所需列表进行检查,例如
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
'start': '1',
'limit': '5000',
'convert': 'USD',
}
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': 'yourKey',
}
session = Session()
session.headers.update(headers)
try:
response = session.get(url, params=parameters)
# print(response.text)
data = json.loads(response.text)
print(data['data'][64]['quote']['USD']['price'])
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
对于您当前的示例:
您可以为interested = ['Electroneum','Ethereum']
for item in data['data']:
if item['name'] in interested:
print(item)
使用属性选择器
data-currency-value
答案 1 :(得分:0)
您可以这样获得值:
import requests
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/currencies/electroneum/'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
price = soup.find("span", id="quote_price").get('data-usd')
print (price)
答案 2 :(得分:0)
您应该尝试更具体地查找商品。
您当前正在使用soup.find('')
,在编写data-currency-price data-usd =
这个ID是类名吗?
为什么不尝试使用ID查找商品。
soup.find(id="link3")
或通过标签查找
soup.find("relevant tag name like div or a")
或类似的东西
find_this = soup.find("a", id="ID HERE")
答案 3 :(得分:0)
import requests
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/currencies/electroneum/'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
x=soup(id="quote_price").text
print (x)
更好地查找ID,或搜索soup.find_all(text="data-currency-price data-usd")[1].text
答案 4 :(得分:0)
您可以使用class属性获取值。
import requests
from bs4 import BeautifulSoup
url = 'https://coinmarketcap.com/currencies/electroneum/'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
price = soup.find('span' ,attrs={"class" : "h2 text-semi-bold details-panel-item--price__value"})
print (price.text)
输出:
0.006778