Python 删除基本url的步骤

Python 删除基本url的步骤,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我编写了一个python脚本,从给定网页上的所有链接中提取href值: from BeautifulSoup import BeautifulSoup import urllib2 import re html_page = urllib2.urlopen("http://kteq.in/services") soup = BeautifulSoup(html_page) for link in soup.findAll('a'): print link.get('href') 当我

我编写了一个python脚本,从给定网页上的所有链接中提取
href
值:

from BeautifulSoup import BeautifulSoup
import urllib2
import re

html_page = urllib2.urlopen("http://kteq.in/services")
soup = BeautifulSoup(html_page)
for link in soup.findAll('a'):
    print link.get('href')
当我运行上述代码时,我得到以下输出,其中包括外部和内部链接:

index
index
#
solutions#internet-of-things
solutions#online-billing-and-payment-solutions
solutions#customer-relationship-management
solutions#enterprise-mobility
solutions#enterprise-content-management
solutions#artificial-intelligence
solutions#b2b-and-b2c-web-portals
solutions#robotics
solutions#augement-reality-virtual-reality`enter code here`
solutions#azure
solutions#omnichannel-commerce
solutions#document-management
solutions#enterprise-extranets-and-intranets
solutions#business-intelligence
solutions#enterprise-resource-planning
services
clients
contact
#
#
#
https://www.facebook.com/KTeqSolutions/
#
#
#
#
#contactform
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
index
services
#
contact
#
iOSDevelopmentServices
AndroidAppDevelopment
WindowsAppDevelopment
HybridSoftwareSolutions
CloudServices
HTML5Development
iPadAppDevelopment
services
services
services
services
services
services
contact
contact
contact
contact
contact
None
https://www.facebook.com/KTeqSolutions/
#
#
#
#

我想删除包含完整URL的外部链接,如
https://www.facebook.com/KTeqSolutions/
同时保留
解决方案#物联网等链接
。我如何才能有效地做到这一点?

如果我理解正确,您可以尝试以下方法:

l = []
for link in soup.findAll('a'):
    print link.get('href')
    l.append(link.get('href'))
l = [x for x in l if "www" not in x] #or 'https'

您可以使用
请求
模块中的
解析url

import requests

url = 'https://www.facebook.com/KTeqSolutions/'

requests.urllib3.util.parse_url(url)
给你

Url(scheme='https', auth=None, host='www.facebook.com', port=None, path='/KTeqSolutions/', query=None, fragment=None)

对不起,“基本链接”是什么?你已经试过什么了?请提供一个最小的示例基本上,基本URL是您网址的一致部分。我想从上述输出中删除,所以您想删除所有以
https
开头的行吗?ReGEX在这里会做的很好。谢谢你的回复。我有OutPut.@ N.NANHYI,欢迎你:如果答案是你正在寻找的,考虑通过点击答案左边的记号来接受它,但是你必须考虑所有的可能性,如:代码> http://< /COD>,<代码> http://,<代码>文件://< /COD>等等。而且它不适用于端口,例如
:8000
:4567
,…非常感谢您的回复。