【问题标题】:How to Identify HTML attributes (Web Scraping)如何识别 HTML 属性(网页抓取)
【发布时间】:2020-07-30 00:55:35
【问题描述】:

我正在构建一个网络爬虫,我不知道如何辨别元素属性。

目前,我正在从左侧移动一个“扫描头”来编写代码,并寻找某些字符串来标记属性。例如,到目前为止,我一直在寻找 =" 来决定某个属性可能存在的位置。

问题已经开始出现,因为编写 HTML 有多种“有效”方式。

例如,来自this home depot page,源代码有两个特定元素:

<a href="https://www.homedepot.com/b/Featured-Products-Ryobi/N-5yc1vZ1z18hrf?cm_sp=vanity-_-ryobi-_-APR19" target="_blank" style="color:#F96302;">Learn More about the RYOBI Platform Here</a>
<a href=https://www.homedepot.com/c/electronics_recycling_programs style=color:#F96302; target=_blank>Click here for more information on Electronic Recycling Programs</a>

这让刮刀很头疼。第一个元素被抓取,但第二个元素没有任何=" 可以找到。我也不能只查找=,因为这会产生误报,就像在第一个元素中有...sp=vanity... 无法正确解析。

如何处理 HTML 的多种语法?

编辑:到目前为止我一直在使用 C++

【问题讨论】:

  • 您使用哪种语言?
  • 好的,谢谢,我正在使用 C++

标签: html c++ web web-scraping attributes


【解决方案1】:

假设答案

我有一个想法,但我不确定它的效果如何。我可以尝试根据空格" " 提取子字符串,然后查找= 的第一个实例

例如:

&lt;a href="foo" target="bar"&gt; -> 根据" "拉取子串得到attList['href="foo"', 'target="bar"']

然后foreach(string att, attList),设置key = att.left(att.indexOf('='))value = att.right(att.indexOf('='))

【讨论】:

    猜你喜欢
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2019-01-20
    • 2018-01-06
    • 2020-10-05
    相关资源
    最近更新 更多