beautifulsoup 和 selenium:单击 svg 路径以转到下一页并从该页面获取数据

beautifulsoup and selenium: clicking on an svg path to get to the next page and get data from that page

我正在做一个项目,在一个充满数据的网站上有一个 table,table 有 7 页长。它是本网站上的 table:https://nonfungible.com/market/history。您通过 svg 路径到达下一页。我必须从所有 7 页中获取数据。我不知道如何点击这个 svg 路径。如果您知道如何单击该路径,请告诉我。即使 svg 没有 aria-label 或 class.

这是源代码的照片。

我尝试过很多不同的东西,包括:

    driver.find_element_by_xpath('//div[@id="icon-chevron-right"]/*[name()="svg"]/*[name()="path"]').click()

这是我收到的错误:raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"xpath","selector":"//div[@id="icon-chevron-right"] /[name()="svg"]/[name()="path"]"} (会话信息:chrome=92.0.4515.107)

感谢您的帮助。请帮我解决这个问题。

您可以尝试使用浏览器的开发人员工具来查看向网站发出的请求,然后从您的脚本中发出相同的请求。这消除了对 selenium 的需求,应该会让你的机器人更具可扩展性。

与使用 GUI 的方法略有不同 - 但请看下面的内容。这样做提供的数据比前端显示的多得多。

看起来 /market/history 页面带有 JSON 数据(它不是单独的调用,可以在开发工具中识别)。 但是 - 如果您:

  1. 获取包含 python requests 库的页面
  2. 解析 html 并找到 json 数据对象,即 @id="__NEXT_DATA__"
  3. 获取 json 的右侧部分,其中包含 table 数据
  4. 过滤对象以去除一些点点滴滴(其中 name != none
from lxml import html
import requests
import json

url = "https://nonfungible.com/market/history"

#get the page and parse
response = requests.get(url)
page = html.fromstring(response.content)

#get the data and convert to json
datastring = page.xpath('//script[@id="__NEXT_DATA__"]/text()')
data = json.loads(datastring[0])
#print(json.dumps(data, indent=4)) #this prints everything

#Get the relevant part of the json (it has lots of other cr*p in there - it was effort to find this
tabledata = data['props']['pageProps']['currentTotals']
# this filters out some of the unneeded data
AllItems = list(filter(lambda x: x['name'] !=None, tabledata)) 

#print out each item - which relates to an row in the table 
for item in  AllItems:
    print (item['name'])
    print (item['totals']['alltime']['usd'])
    print (json.dumps(item, indent=4))

从这里您需要做的是从 json.

中提取您想要的内容

我已经开始了你... 循环中的前 2 个打印输出如下:

meebits

157251919.08

与网站上的项目匹配:

最后一张印刷品就是这件物品的全部。这将使您看到结构并帮助您获取数据。它看起来像这样:

{
    "name": "meebits",
    "totals": {
        "alltime": {
            "count": 15622,
            "traders": 5023,        
            "usd": 157251919.08,    
            "average": 10066.06,    
            "transfer_count": 29826,
            "transfer_unique_assets": 19981,
            "asset_unique_owners": 4812,
            "asset_usd": 95541331.68,
            "asset_average": 10566.39
        },
        "oneday": {
            "count": 0,
            "traders": 0,
            "usd": 0,
            "average": 0,
            "transfer_count": 0,
            "transfer_unique_assets": 0,
            "asset_unique_owners": 0,
            "asset_usd": 0,
            "asset_average": 0
        },
        "twodayago": {
            "count": 0,
            "traders": 0,
            "usd": 0,
            "average": 0
        },
        "sevenday": {
            "count": 144,
            "traders": 165,
            "usd": 703913.21,
            "average": 4888.29,
            "transfer_count": 265,
            "transfer_unique_assets": 204,
            "asset_unique_owners": 125,
            "asset_usd": 611620.92,
            "asset_average": 5412.57
        },
        "thirtyday": {
            "count": 1663,
            "traders": 1167,
            "usd": 12662841.8,
            "average": 7614.46,
            "transfer_count": 2551,
            "transfer_unique_assets": 1704,
            "asset_unique_owners": 781,
            "asset_usd": 9908945.2,
            "asset_average": 9107.49
        }
    }
}