【问题标题】:Trim links in list with python3.7用python3.7修剪列表中的链接
【发布时间】:2019-01-12 19:21:27
【问题描述】:

我在 python3.7 中有一个小脚本(请参阅相关问题 here),它从网站 (http://digesto.asamblea.gob.ni/consultas/coleccion/) 中抓取链接并将它们保存在列表中。不幸的是,它们只是部分的,我必须修剪它们以将它们用作链接。

这是脚本的相关部分:

list_of_links = []    # will hold the scraped links
tld = 'http://digesto.asamblea.gob.ni'
current_url = driver.current_url   # for any links not starting with /
table_id = driver.find_element(By.ID, 'tableDocCollection')
rows = table_id.find_elements_by_css_selector("tbody tr") # get all table rows
for row in rows:
    row.find_element_by_css_selector('button').click()
    link = row.find_element_by_css_selector('li a[onclick*=pdf]').get_attribute("onclick") # href
    print(list_of_links)# trim
    if link.startswith('/'):
        list_of_links.append(tld + link)
    else:
        list_of_links.append(current_url + link)
    row.find_element_by_css_selector('button').click()

print(list_of_links)

我怎样才能操作这个列表(这里仅以三个条目为例)

["http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D');return false;", "http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=Z%2FgLeZxynkg%3D');return false;", "http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=9rka%2BmYwvYM%3D');return false;"]

看起来像

["http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D", "http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=Z%2FgLeZxynkg%3D", "http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=9rka%2BmYwvYM%3D"]

分解:在第一个链接的例子中,我从网站上得到这个链接基本上是

http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D');return false;

并需要将其修剪为

http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D.

如何在 python 中从整个列表中实现这一点?

【问题讨论】:

  • .replace("/consultas/coleccion/window.open('", "") 将帮助您开始
  • 你能做出什么样的假设?例如,您的所有链接都以 3D 完成吗?他们都有window.open吗?

标签: python python-3.x


【解决方案1】:

一种方法是在字符串/consultas/coleccion/window.open(' 上使用split,删除第二个字符串不需要的结尾,然后连接两个处理过的字符串以获得结果。

应该这样做:

new_links = []

for link in list_of_links:

    current_strings = link.split("/consultas/coleccion/window.open('")
    current_strings[1] = current_strings[1].split("');return")[0]
    new_link = current_strings[0] + current_strings[1]
    new_links.append(new_link)

【讨论】:

    【解决方案2】:

    这应该可以解决问题:

    s = "http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D');return false;"
    s = s.replace("/consultas/coleccion/window.open('", "").replace("');return false;", "")
    

    【讨论】:

      【解决方案3】:

      您可以使用正则表达式来拆分列表中的 URL,并让 urllib.parse.urljoin() 为您完成其余部分:

      import re
      from urllib.parse import urljoin
      
      PATTERN = r"^([\S]+)window.open\('([\S]+)'"
      
      links = ["http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D');return false;"]
      result = "http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D"
      
      for link in links:
          m = re.match(PATTERN, link, re.MULTILINE).groups()
          #  m is now: ('http://digesto.asamblea.gob.ni/consultas/coleccion/', '/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D')
          if len(m) == 2:
              newLink = urljoin(*m)
              print(newLink)
              assert newLink == result
      

      返回:

      http://digesto.asamblea.gob.ni/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D
      

      【讨论】:

        【解决方案4】:

        你可以使用正则表达式:

        考虑这段代码:

        import re
        out = list()
        lst = ["http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=p2%2FHzlqau8A%3D');return false;", "http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=Z%2FgLeZxynkg%3D');return false;", "http://digesto.asamblea.gob.ni/consultas/coleccion/window.open('/consultas/util/pdf.php?type=rdd&rdd=9rka%2BmYwvYM%3D');return false;"]
        
        for el in lst:
            temp = re.sub(r"(.*?)/window.open\('(.*?)'\).*", r"\1\2", el)
            out.append(temp)
            print(temp)
        

        函数sub 允许替换与指定模式匹配的部分字符串。基本上就是这样:

        • (.*?):保留/window.open...之前的所有字符
        • /window.open\( 输入字符串必须具有/window.open( 模式,但不会保留
        • (.*?) 保留前一个模式之后的所有字符,直到找到)(由\( 表示)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-04-27
          • 2018-05-02
          • 2020-05-21
          • 1970-01-01
          • 2018-03-11
          • 1970-01-01
          相关资源
          最近更新 更多