【问题标题】:BeautifulSoup WebScraping Issue: Cannot find specific classes for this specific Website (Python 3.7)BeautifulSoup WebScraping 问题:找不到此特定网站的特定类(Python 3.7)
【发布时间】:2021-07-14 02:11:50
【问题描述】:

我对 webscraping 有点陌生,我之前使用以下方法创建了 webscrapers,但是对于这个特定的网站,我遇到了解析器无法找到特定类('mainTitle___mbpq1')的问题,这是类指公告正文。每当我运行代码时,它都会返回 None。大多数其他类也是如此。我想在不使用 selenium 的情况下捕获此信息,因为这会减慢我所理解的过程。我认为问题在于它是一个 json 文件,因此正在使用脚本标签(我可能完全错了,只是一个猜测),但我对这方面了解不多,所以任何帮助将不胜感激。

我尝试使用下面的代码,但没有成功。

from bs4 import BeautifulSoup
import re
import requests
# Method 1
url_4 = "https://www.kucoin.com/news/categories/listing"
res = requests.get(url_4)
html_page = res.content
soup = BeautifulSoup(html_page, 'html.parser')
texts = soup.body
text = soup.body.div.find('div',{'class':'mainTitle___mbpq1'})
print(text)


from bs4 import BeautifulSoup
import urllib3
import re
# Method2
http = urllib3.PoolManager()
comm = re.compile("<!--|-->")
def make_soup(url):
    page = http.request('GET', url)
    soupdata = BeautifulSoup(page.data,features="lxml")
    return soupdata

soup = make_soup(url_4)
Annouce_Info = soup.find('div',{'class':'mainTitle___mbpq1'})

print(Annouce_Info)

链接KuCoin Listing

【问题讨论】:

    标签: python-3.x web-scraping beautifulsoup


    【解决方案1】:

    数据是通过 Javascript 从外部源加载的。要打印所有文章标题,您可以使用以下示例:

    import json
    import requests
    
    
    url = "https://www.kucoin.com/_api/cms/articles"
    params = {"page": 1, "pageSize": 10, "category": "listing", "lang": ""}
    data = requests.get(url, json=params).json()
    
    # uncomment this to print all data:
    # print(json.dumps(data, indent=4))
    
    for item in data["items"]:
        print(item["title"])
    

    打印:

    PhoenixDAO (PHNX) Gets Listed on KuCoin!
    LABS Group (LABS) Gets Listed on KuCoin! World Premiere!
    Polkadex (PDEX) Gets Listed on KuCoin! World Premiere!
    Announcement of Polkadex (PDEX) Token Sale on KuCoin Spotlight
    KuCoin Futures Has Launched USDT Margined NEO, ONT, XMR, SNX Contracts
    Introducing the Polkadex (PDEX) Token Sale on KuCoin Spotlight
    Huobi Token (HT) Gets Listed on KuCoin!
    KuCoin Futures Has Launched USDT Margined XEM, BAT, XTZ, QTUM Contracts
    RedFOX Labs (RFOX) Gets Listed on KuCoin!
    Boson Protocol (BOSON) Gets Listed on KuCoin! World Premiere!
    

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    如果您尝试抓取有关加密货币交易所新上市的信息,您可能会对这个 API 感兴趣:

    https://rapidapi.com/Diver44/api/new-cryptocurrencies-listings/

    import requests
    
    url = "https://new-cryptocurrencies-listings.p.rapidapi.com/new_listings"
    
    headers = {
        'x-rapidapi-host': "new-cryptocurrencies-listings.p.rapidapi.com",
        'x-rapidapi-key': "your-key"
        }
    
    response = requests.request("GET", url, headers=headers)
    
    print(response.text)
    

    它包括一个端点,其中包含来自最大交易所的新列表和一个非常有用的端点,其中包含有关您可以在该交易所购买特定硬币和该硬币价格的交易所的信息

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多