【问题标题】:Extracting links with regex from source code; Python从源代码中提取带有正则表达式的链接; Python
【发布时间】:2014-09-29 17:11:48
【问题描述】:

我有一个指向报纸文章链接的数据集,我想对其进行一些研究。但是,数据集中的链接以 .ece 扩展名结尾(由于一些 api 限制,这对我来说是个问题)

http://www.telegraaf.nl/telesport/voetbal/buitenlands/article22178882.ece

http://www.telegraaf.nl/telesport/voetbal/buitenlands/22178882/__Wenger_vreest_het_ergste__.html

是指向同一页面的链接。 现在我需要将所有 .ece 链接转换为 .html 链接。我没有找到更简单的方法,而是解析页面并找到原始 .html 链接。问题是链接隐藏在 html 元元素中,我无法使用 tree.xpath 访问它。

<meta content="http://www.telegraaf.nl/telesport/voetbal/buitenlands/22178882/__Wenger_vreest_het_ergste__.html"

不幸的是,我不太熟悉正则表达式,也不知道如何使用它来提取链接。 基本上,我需要的每个链接都以:

<meta content="http://www.telegraaf.nl/

我需要完整链接(即http://www.telegraaf.nl/THE_REST_OF_THE_LINK)。 另外,我正在使用 BeautifulSoup 进行解析。谢谢。

【问题讨论】:

  • 如果给你正则表达式字符串,你至少知道如何使用正则表达式吗?
  • 好吧,我知道我必须使用 re 模块。 re.findall (r"expression", "string")?
  • 你是如何获得链接的?
  • @Padraic Cunningham,我有一个专门包含 .ece 链接的文件。当我打开页面的源代码时,我发现它们将 .html 链接存储在元元素中。我需要的是从源代码中获取该链接(.html)。
  • @Zlo 我认为您可能可以调整我的答案...我假设它们都在同一个文件中...但是您可以根据需要调整它.. .

标签: python html regex xpath


【解决方案1】:

这是一个非常简单的正则表达式,可以帮助您入门。

This one 将提取所有链接

\<meta content="(http:\/\/www\.telegraaf\.nl.*)"

这个会匹配所有的html链接

\<meta content="(http:\/\/www\.telegraaf\.nl.*\.html)"

要将其与您所拥有的一起使用,您可以执行以下操作:

import urllib2
import re

replacements = dict()
for url in ece_url_list:
    response = urllib2.urlopen(url)
    html = response.read()
    replacements[url] = re.findall('\<meta content="(http:\/\/www\.telegraaf\.nl.*\.html)"', html)[0]

注意:这假定每个源代码页面始终在此元标记中包含一个 html 链接。它预计只有一个。

【讨论】:

  • 谢谢,这正是我需要的!
【解决方案2】:

使用 BeautifulSoup 查找匹配的内容属性,然后替换为:

from bs4 import BeautifulSoup
import re

html = """
    <meta content="http://www.telegraaf.nl/telesport/voetbal/buitenlands/article22178882.ece" />
    <meta content="http://www.telegraaf.nl/telesport/voetbal/buitenlands/22178882/__Wenger_vreest_het_ergste__.html" />
"""

soup = BeautifulSoup(html)
# reference table of url prefixes to full html link
html_links = {
    el['content'].rpartition('/')[0]: el['content'] 
    for el in soup.find_all('meta', content=re.compile('.html$'))
}
# find all ece links, strip the end of to match links, then adjust
# meta content with looked up element
for el in soup.find_all('meta', content=re.compile('.ece$')):
    url = re.sub('(?:article(\d+).ece$)', r'\1', el['content'])
    el['content'] = html_links[url]

print soup
# <meta content="http://www.telegraaf.nl/telesport/voetbal/buitenlands/22178882/__Wenger_vreest_het_ergste__.html"/>

【讨论】:

    【解决方案3】:
    (.*?)(http:\/\/.*\/.*?\.)(ece)
    

    试试这个。替换为$2html

    查看演示。

    http://regex101.com/r/nA6hN9/24

    【讨论】:

    • 我认为这行不通。 OP显示html链接与ece链接不同。它们不仅仅是.html。换句话说,对 .ece 进行简单的查找替换是行不通的。他需要用整个第二个链接替换整个链接。
    • @Humdinger m 现在丢失了。将等待 OP 的澄清
    • @Humdinger,没错。 .html 链接与 .ece 链接有点不同,因此简单的替换不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 2014-03-22
    • 1970-01-01
    相关资源
    最近更新 更多