Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python建议:编译包含关键字但仅包含唯一有效链接的URL列表_Python_Beautifulsoup - Fatal编程技术网

Python建议:编译包含关键字但仅包含唯一有效链接的URL列表

Python建议:编译包含关键字但仅包含唯一有效链接的URL列表,python,beautifulsoup,Python,Beautifulsoup,我仍然在学习Python,所以我希望有人能帮助我解决这个问题,避免一些手动步骤 我需要编译/创建表单列表 Institution, LinkToSpecificWebPage 其中链接包含一个特定的工作(对于这个参数,让我们说“存储”) 我面临的问题是,每个机构都可以对我需要的网页的网址使用不同的形式或结构(一些使用store.Institution.tld,另一些使用Institution.tld/store) 此外,根据机构使用的网络平台,它可能是完整的url,也可能是相对链接,如/st

我仍然在学习Python,所以我希望有人能帮助我解决这个问题,避免一些手动步骤

我需要编译/创建表单列表

Institution, LinkToSpecificWebPage 
其中链接包含一个特定的工作(对于这个参数,让我们说“存储”)

我面临的问题是,每个机构都可以对我需要的网页的网址使用不同的形式或结构(一些使用store.Institution.tld,另一些使用Institution.tld/store) 此外,根据机构使用的网络平台,它可能是完整的url,也可能是相对链接,如/store

我已经使用BS4返回了所有包含“store”的链接,并将它们打印出来

我的问题是如何建立一个列表,以便列出每个机构,并且每个机构都有一个指向其商店的完整url

对于http或www字符串,是否有比if语句和测试更优雅的方法

找到的链接示例 该代码输出机构名称、机构主页(均来自早期流程,然后从每个主页输出包含我感兴趣的文本的链接)。 我已经删除了一些重复的链接,但正如您所想象的,有时同一个链接会在页面上多次出现,因此可能会返回格式良好的完整链接,但不一定是第一个

InstName,HomePage.url,link.get('href')

Marino Institute http://www.mie.ie/ /Library.aspx
TCD http://www.tcd.ie/ /Library/
DIT http://www.dit.ie/ http://dit.ie/library/
IT Tallaght http://www.it-tallaght.ie/ libraryservice3
LYIT http://www.lyit.ie/#!prettyPhoto http://library1.lyit.ie/
DCU http://www.dcu.ie/ /library/index.shtml
NUIG http://www.nuigalway.ie/ //www.library.nuigalway.ie/
使用PythonUrlParse(请参阅),您可以解析每个部分url,然后将这些部分重新组合成一个url

from urlparse import urlsplit, urlunsplit

data = [["Marino Institute","http://www.mie.ie/","/Library.aspx"],
["TCD", "http://www.tcd.ie/", "/Library/"],
["DIT", "http://www.dit.ie/", "http://dit.ie/library/"],
["IT Tallaght", "http://www.it-tallaght.ie/", "libraryservice3"],
["LYIT", "http://www.lyit.ie/#!prettyPhoto", "http://library1.lyit.ie/"],
["DCU", "http://www.dcu.ie/", "/library/index.shtml"],
["NUIG", "http://www.nuigalway.ie/", "//www.library.nuigalway.ie/"]]


def merge_urls(partial_url1, partial_url2):

    o1 = urlsplit(partial_url1)
    o2 = urlsplit(partial_url2)

    # domain of o2 takes precedence over domain in o1
    if(o2.netloc != ''):
        o3 = o2
        o2 = o1
        o1 = o3

    schemes, netlocs, paths, queries, fragments = zip(o1, o2)
    scheme = schemes[0] if schemes[0] != '' else schemes[1]
    netloc = netlocs[0] if netlocs[0] != '' else netlocs[1]
    path = paths[0] if paths[0] != '/' else paths[1]
    query = queries[0] if queries[0] != '' else queries[1]
    fragment = fragments[0] if fragments[0] != '' else fragments[1]

    return urlunsplit((scheme, netloc, path, query, fragment))

for d in data:
    print d[0], merge_urls(d[1], d[2])
这个输出

Marino Institute http://www.mie.ie/Library.aspx
TCD http://www.tcd.ie/Library/
DIT http://dit.ie/library/
IT Tallaght http://www.it-tallaght.ie/libraryservice3
LYIT http://library1.lyit.ie/#!prettyPhoto
DCU http://www.dcu.ie/library/index.shtml
NUIG http://www.library.nuigalway.ie/

你能添加一个你正在抓取的页面片段,显示一些这些机构及其链接吗?