我有以下由不同字符串组成的凌乱数据示例。我想将网址转换为包含协议和给定路径的网站格式类型。排除其他人次要重要
以下是熊猫系列:
0 None
1 http://fakeurl.com/example/fakeurl
2 https://www.qwer.com/example/qwer
3 None
4 test.com/example/test
5 None
6 123135123
7 nourlhere
8 lol
9 hello.tv
10 nolink
11 ihavenowebsite.com
在我的代码中,我首先想将所有url转换为简单的domain.com
+路径(如果有),然后使用正则表达式添加协议。在第二个正则表达式中,我想使用以下模式https://www.example.com/example/example
将路径添加到没有路径的路径中,因此路径的结尾应重复域名
代码:
def change_by_regexp(dfc, regexp, string):
dfc[~dfc.str.match(regexp)==False] = string
example = pd.Series(['None', 'http://fakeurl.com/example/fakeurl', 'https://www.qwer.com/example/qwer', 'None', 'test.com/example/test', 'None', '123135123', 'nourlhere', 'lol', 'hello.tv', 'nolink', 'ihavenowebsite.com'])
example = example.map(lambda x: x.replace('https://www.', ''))
example = example.map(lambda x: x.replace('www.', ''))
example = example.map(lambda x: x.replace('https://', ''))
example = example.map(lambda x: x.replace('http://', ''))
change_by_regexp(example, r'([-a-zA-Z0-9\u0080-\u024F@:%._\+~#=]{1,256})\.[a-zA-Z0-9()]{1,6}\b','http://www.' + example)
change_by_regexp(example, r'^((http[s]?|ftp):\/)?\/?([-a-zA-Z0-9\u0080-\u024F@:%._\+~#=]{1,256})\.[a-zA-Z0-9()]{1,6}\b$', example + '/example/')
print(example)
输出:
0 None
1 http://www.fakeurl.com/example/fakeurl
2 http://www.qwer.com/example/qwer
3 None
4 http://www.test.com/example/test
5 None
6 123135123
7 nourlhere
8 lol
9 http://www.hello.tv/example/
10 nolink
11 http://www.ihavenowebsite.com/example/
dtype: object
现在是否可以使用主机名并在路径末尾将其返回?通过使用另一个正则表达式来搜索主机名并返回它,是否有可能做到这一点?我根本找不到合适的解决方案。到达我的...
预期输出:
0 None
1 http://www.fakeurl.com/example/fakeurl
2 http://www.qwer.com/example/qwer
3 None
4 http://www.test.com/example/test
5 None
6 123135123
7 nourlhere
8 lol
9 http://www.hello.tv/example/hello
10 nolink
11 http://www.ihavenowebsite.com/example/ihavenowebsite
dtype: object
答案 0 :(得分:1)
example = pd.Series(['None', 'http://fakeurl.com/example/fakeurl', 'https://www.qwer.com/example/qwer',
'None', 'test.com/example/test',
'None', '123135123', 'nourlhere', 'lol', 'hello.tv', 'nolink', 'ihavenowebsite.com'])
example=pd.DataFrame(example.rename('main'))
example['path']=example['main']\
.str.replace('http://','')\
.str.replace('www.','')\
.str.replace('https://','')
#take only rows with adres( and extract this adres)
example.loc[
example.path.str.contains('\.')
,'host']=example.loc[example.path.str.contains('\.'),'path'].str.split('.').apply(lambda x: x[0])
example['host'] = '/example/'+example['host']
#add path where is not example/host_name
example.loc[
~example.main.str.contains('/example/'),'main']=example.loc[
~example.main.str.contains('/example/'),'main']+example.loc[
~example.main.str.contains('/example/'),'host']
example.loc[example.main.isna(),'main'] = example.loc[example.main.isna(),'path']
example=example[['main']]
print(example)
main
0 None
1 http://fakeurl.com/example/fakeurl
2 https://www.qwer.com/example/qwer
3 None
4 test.com/example/test
5 None
6 123135123
7 nourlhere
8 lol
9 hello.tv/example/hello
10 nolink
11 ihavenowebsite.com/example/ihavenowebsite
答案 1 :(得分:1)
稍微重构代码以使其更具可读性。我用urllib.parse
做最后一部分。
mport re
import urllib.parse
example = pd.Series(['None', 'http://fakeurl.com/example/fakeurl', 'https://www.qwer.com/example/qwer', 'None', 'test.com/example/test', 'None', '123135123', 'nourlhere', 'lol', 'hello.tv', 'nolink', 'ihavenowebsite.com'])
re1 = r'([-a-zA-Z0-9\u0080-\u024F@:%._\+~#=]{1,256})\.[a-zA-Z0-9()]{1,6}\b'
re2 = r'^((http[s]?|ftp):\/)?\/?([-a-zA-Z0-9\u0080-\u024F@:%._\+~#=]{1,256})\.[a-zA-Z0-9()]{1,6}\b$'
re3 = r'www\.([\w]*)'
def modurl(s):
u = urllib.parse.urlparse(s)
if u.netloc=="" or u.path!="/example":
return s
else:
return f"{s}/{re.findall(re3, urllib.parse.urlparse(s).netloc)[0]}"
example = (example
.map(lambda x: x.replace('https://www.', ''))
.map(lambda x: x.replace('www.', ''))
.map(lambda x: x.replace('https://', ''))
.map(lambda x: x.replace('http://', ''))
.map(lambda x: np.where(bool(re.search(re1, x)), "http://www."+x, x))
.map(lambda x: np.where(bool(re.search(re2, x)), x+"/example", x))
.map(lambda x: modurl(x))
)
print(example.to_string())
输出
0 None
1 http://www.fakeurl.com/example/fakeurl
2 http://www.qwer.com/example/qwer
3 None
4 http://www.test.com/example/test
5 None
6 123135123
7 nourlhere
8 lol
9 http://www.hello.tv/example/hello
10 nolink
11 http://www.ihavenowebsite.com/example/ihavenow...