我正试图抓取该网站,以便下载该网站上列出的所有硬币的名称和链接:https://www.cryptocompare.com/ico/#/completed
我尝试使用beautifulsoup模块找到div class = coins-list,然后找到
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
url= "https://www.cryptocompare.com/ico/#/completed"
r=session.get(url)
soup = BeautifulSoup(r.text,'html.parser')
coin_name_list = soup.find("div",attrs= {"id":"coins-list"})
coin_name_list_items = coin_name_list.find_all('a')
错误消息如下: ---> 14个coin_name_list_items = coin_name_list.find_all('a')
AttributeError:“ NoneType”对象没有属性“ find_all”
我不明白为什么这行不通,与网站或程序有关吗?如果有人可以找到解决方案,将不胜感激。谢谢!
答案 0 :(得分:0)
JavaScript渲染页面。但是,如果您转到“网络”选项卡,则会在api下面获得,该api将输出返回为json
url="https://min-api.cryptocompare.com/data/subsWatchlist?fsyms=433,ABT,DRG,XTZ,FIL,BNT,SPICE,ALGO,SRN,PMA,SNT,SOLID,WAX,NTK,BKX,DRT,EVN,ONL,OIO,LDC,CEL,BUBO,SVD,DOR,ICX,INS,LAX,GAT,NGC,WPR,OKOIN,HBZ,BQTX,MTH,PAT,BAT,AST,OMX,INVC,C20,POWR,FUEL,REQ,CVC,AGI,AMB,LINK,BRD,PLBT,SYNCO,STORJ,MWAT,LUC,CRE,CMT,BMC,BERRY,redBUX,RAC,MCO,DLT,STX,SFU,TCOIN,RKT,MANA,OAK,SNM,FUN,WRC,BCAT,VRA,CFTY,TNT,PROPS,OMG,ANT,AE,EDO,SETH,WT,TIOX,CMCT,ENJ,EVX,KICK,COV,ZUP,PLR,VEE,BDG,KEY,STT,U42,UTK,AID,AVT,IMPCN,TFLEX,SLX,PLA,ISR,GARD,FLUZ,BAX,RVT,CSP,HOLO,MCAP,THO,VLR,TFD,WBBC,ELY,DBCCOIN,LA,OAX,DAV,ETH,OST,DMT,GLDR,IHT,FLP,CTR,ILCT,BEX,CTLX,ATOM,NAVI,CEDEX,EVC,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2,DFBT,OXY2,MIOTA,ZYM,ZUC,ZRX,ZNT,ZNAQ,ZNA,ZIX,ZILLA,ZEROB,ZEEW,ZCHN,ZCC1,ZAZA,ZAB,YSH,YPTO,YDY,YBT,YANU,YACHTCO,XUC,XTN,XSB,XRK,XRF,XRED,XRBT,XR,XPX,XPT,XPR,XPL,XOS,XNT,XIM,XGH,XEP,XDT,XCZ,XBX,XBOND,XBANK,XAL,WYR,WXT,WUG,WU,WTXH,WTT,WTL,WSH,WRT,WPT,WPP,WOWX,WORK,WOM,WMK,WMD,WMB,WLME,WIIX,WICC&tsym=USD"
导入json以使用该json对象。
import requests
import json
headers = {'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}
url="https://min-api.cryptocompare.com/data/subsWatchlist?fsyms=433,ABT,DRG,XTZ,FIL,BNT,SPICE,ALGO,SRN,PMA,SNT,SOLID,WAX,NTK,BKX,DRT,EVN,ONL,OIO,LDC,CEL,BUBO,SVD,DOR,ICX,INS,LAX,GAT,NGC,WPR,OKOIN,HBZ,BQTX,MTH,PAT,BAT,AST,OMX,INVC,C20,POWR,FUEL,REQ,CVC,AGI,AMB,LINK,BRD,PLBT,SYNCO,STORJ,MWAT,LUC,CRE,CMT,BMC,BERRY,redBUX,RAC,MCO,DLT,STX,SFU,TCOIN,RKT,MANA,OAK,SNM,FUN,WRC,BCAT,VRA,CFTY,TNT,PROPS,OMG,ANT,AE,EDO,SETH,WT,TIOX,CMCT,ENJ,EVX,KICK,COV,ZUP,PLR,VEE,BDG,KEY,STT,U42,UTK,AID,AVT,IMPCN,TFLEX,SLX,PLA,ISR,GARD,FLUZ,BAX,RVT,CSP,HOLO,MCAP,THO,VLR,TFD,WBBC,ELY,DBCCOIN,LA,OAX,DAV,ETH,OST,DMT,GLDR,IHT,FLP,CTR,ILCT,BEX,CTLX,ATOM,NAVI,CEDEX,EVC,TPAY,WAVES,WELL,WHN,LYK,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,ZUC,ZRX,ZNT,ZNAQ,ZNA,ZIX,ZILLA,ZEROB,ZEEW,ZCHN,ZCC1,ZAZA,ZAB,YSH,YPTO,YDY,YBT,YANU,YACHTCO,XUC,XTN,XSB,XRK,XRF,XRED,XRBT,XR,XPX,XPT,XPR,XPL,XOS,XNT,XIM,XGH,XEP,XDT,XCZ,XBX,XBOND,XBANK,XAL,WYR,WXT,WUG,WU,WTXH,WTT,WTL,WSH,WRT,WPT,WPP,WOWX,WORK,WOM,WMK,WMD,WMB,WLME,WIIX,WICC&tsym=USD"
r=requests.get(url,headers=headers)
data=json.loads(r.text)
print(data)
但是如果您想使用代码,则可能无法获得所有值,因为页面是由javascripts呈现的。请使用selenium或我上面的解决方案来获得预期的结果。要获取所有链接,只需使用soup.find_all('a')
`
这是代码。
from bs4 import BeautifulSoup
from requests_html import HTMLSession
session = HTMLSession()
url= "https://www.cryptocompare.com/ico/#/completed"
r=session.get(url)
soup = BeautifulSoup(r.text,'html.parser')
coin_name_list_items = soup.find_all('a')