python - 试图刮掉 coinmarketcap 并获得“无”
问题描述
我正在使用 python 构建一个基本脚本来从coinmarketcap中抓取一些数据,但我没有得到任何数据,我不知道为什么,请你帮帮我?
from bs4 import BeautifulSoup as S
import requests
c = input('enter your coin')
url = f'https://coinmarketcap.com/currencies/{c}/'
headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
r = requests.get(url,headers=headers)
soup = S(r.content,'html.parser')
print(f'the price of {c} now is ')
x = soup.find(id="priceValue___11gHJ")
print(x)```
解决方案
我已经测试了您的代码并进行了一些更改以使其正常工作。
您要获取的字段使用类而不是 id。
看看它。
#!/usr/bin/env python3
from bs4 import BeautifulSoup as S
import requests
c = input('enter your coin')
url = f'https://coinmarketcap.com/currencies/{c}/'
headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
r = requests.get(url,headers=headers)
soup = S(r.content,'html.parser')
print(f'the price of {c} now is ')
x = soup.find(class_='priceValue___11gHJ').text
print(x)
推荐阅读
- linux - ksmserver-logout-greeter 意外关闭
- visual-studio-code - 如何将live sass编译器的路径设置为VsCode中的当前路径
- javascript - 变量不会显示变化
- windows - Windows 11 中 Visual Studio Code 中的多行编辑
- python - 解包 * 在 leetcode 上抛出语法错误
- azure - kubectl get pods 命令显示“ErrImageNeverPull”
- node.js - 在 Web 数据迁移期间如何组织 AWS S3 存储桶中的数据?
- node.js - 使用路由控制器和模型单独文件时无法从本地 mongoDB 获取集合
- visual-studio-code - Visual Studio Code: How to "save as" all open editor windows
- wordpress - Show posts from two specific category in WP_Query