被简单的网络垃圾缠住了

2024-10-02 20:39:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图从下面的网站上刮取硬币的名字
https://www.block123.com/en/nav/065285354703.htm
我已经拖网了堆栈溢出,但不明白为什么我没有得到
网站上的完整姓名列表。我不熟悉这一点,因此感谢所有人
帮助和耐心

    url = 'https://www.block123.com/en/nav/065285354703.htm'

    r = requests.get(url)

    soup = BeautifulSoup(r.text, 'html.parser')


    for coin in soup.find_all('div', attrs={'class':'portfolio-content item-content'}):
      print(coin.find("div", attrs={"class":"name"}))

   

现在,我已经用selenium编写了一个scraper,使我能够输入密码信息
然后,我可以访问完整的投资组合详细信息,我已经刮去了所有的硬币,但也得到了许多相同类名的不需要的文本。
提供的答案中建议的代码非常有助于删除这些内容,因为它只包含在上一节中有“公文包”的元素
我不确定在使用selenium时如何应用此功能。如果您有任何帮助,我将不胜感激

    driver.get(links['https://www.block123.com/en/nav/065285354703.htm'])
    coins = driver.find_elements_by_class_name("portfolio-item")
    coin_names = [coin.find_element_by_class_name('name').text for coin in coins]

    

Tags: namehttpscomurlget网站www硬币
1条回答
网友
1楼 · 发布于 2024-10-02 20:39:43

检查带有'class':'portfolio-content item-content'的每个div前面的<div>是否包含文本Portfolio,以确定div是否包含硬币,如果是,则提取硬币名称

for item in soup.find_all('div', attrs={'class':'portfolio-content item-content'}):
    if 'Portfolio' in item.find_previous('div').text:
        coins = [coin.text for coin in item.find_all("div", attrs={"class":"name"})]
        print(coins)
['YEECO', 'SoundArio', 'COVA']

相关问题 更多 >