【问题标题】:Puppeteer - how can I find all elements repeated on a page with just one xpath?Puppeteer - 如何仅使用一个 xpath 查找页面上重复的所有元素?
【发布时间】:2022-12-02 19:06:34
【问题描述】:

My app allows users to build webscrapers without code. We want to allow an event called \'get all\' that gets every iteration of that same path in the list.

On this website https://www.worldometers.info/geography/alphabetical-list-of-countries/

Afghanistan would be /html/body/div[3]/div[2]/div[1]/div/div[2]/table/tbody/tr[1]/td[2] Albania /html/body/div[3]/div[2]/div[1]/div/div[2]/table/tbody/tr[2]/td[2]

If I want to query for all, I can manually edit the xpath to be (remove index from last tr) /html/body/div[3]/div[2]/div[1]/div/div[2]/table/tbody/tr/td[2]

the issue is this same structure (using tables) wont be repeated on every site I.e. google page result, instagram user list, etc.

With puppeteer what would be my best method to find \'similar\' items on the page?

  • Please don\'t use these xpaths. If even a single thing changes on the page in that path, the whole thing fails. There are much better ways to select elements, like using classes, properties and ids. That said, every page structure is different so there\'s no such thing as a silver bullet selector that can scrape anything. Tables are pretty generic, so you could try table tr but it\'s still not a silver bullet. What do you mean by \'similar\' items exactly?
  • my issue is some sites having autogenerated class names were breaking the bot from running. What would be my best source for consistently finding/getting what I need? There aren\'t set sites being used so im trying to find the best solution for the majority. Regarding \'similar\' items, I was wondering if a solution would be to compare the xpath I got to another xpath to notice that they have a different index in the last tr thus making the \'list\' just tr instead of tr[index]

标签: web-scraping xpath puppeteer


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 2019-02-05
  • 2022-01-24
  • 2015-08-10
相关资源
最近更新 更多