【问题标题】:Remove duplicate url structure删除重复的 url 结构
【发布时间】:2018-12-11 01:45:14
【问题描述】:

我正在编写一个爬虫,我有一个列表,其中包含一组类似于

的 URL
  • somesite.com/colection/id/index.php?if=12
  • somesite.com/index.php?id=14
  • somesite.com/index.php?id=156
  • example.com/view.php?image=441
  • somesite.com/page.php?id=sas231
  • example.com/view.php?ivideo=4
  • somesite.com/page.php?id=56
  • example.com/view.php?image=1

我想在域之后解析具有相同结构的 url 并获取第一个 url,比如 Burp Suite,它有一个可以删除重复 url(相同参数但值不同)的期货。

  • somesite.com/colection/id/index.php?if=12
  • somesite.com/index.php?id=14
  • example.com/view.php?image=441
  • somesite.com/page.php?id=asa231
  • example.com/view.php?ivideo=4

如您所见,相同但具有不同查询字符串的页面已被删除。这就是我要归档的内容。我尝试了很多正则表达式但没有用。任何人都可以帮助我解决这个问题。提前致谢。 P/s: 对不起我的英语。

【问题讨论】:

  • 请更具体一些(例如,somesite.com/page.php?id=asa231 来自哪里?)并发布您之前的尝试(您尝试过的正则表达式)。
  • 像 Burp Suite 一样,它有一个可以删除重复 url(参数相同但值不同)的期货。

标签: python parsing url


【解决方案1】:

您可以使用urlparse 库将 URL 拆分为多个部分,然后提取您需要的部分。例如:

>>> from urllib.parse import urlparse
>>> urlparse('http://somesite.com/page.php?id=sas231')
ParseResult(scheme='http', netloc='somesite.com', path='/page.php', params='', query='id=sas231', fragment='')

python3 版本库的文档位于urlparse

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2017-02-12
    • 2014-11-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 2023-03-27
    相关资源
    最近更新 更多