import json
import requests
import re
tag = "tag=mirai"
hd = {"Content-Type": "application/x-www-form-urlencoded"}
r = requests.post ('https://urlhaus-api.abuse.ch/v1/tag/', data=tag, headers=hd)
c = ['urls', 'url']
data = json.loads (r.text)
for i in range (0, 998):
fff = data['urls'][i]['url']
pattern = r"((([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])[ (\[]?(\.|dot)[ )\]]?){3}([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]))"
ips = [match[0] for match in re.findall (pattern, fff)]
list2 = []
for item in ips:
if item != []:
list2.append (item)
print(list2)我写了这段代码和输出类型的行列表,我在整个过程中搜索了5天,找到了问题的答案。
输出示例
['51.15.64.60']
['51.15.64.60']
['149.3.170.181']
['149.3.170.181']
['89.248.166.183']
['185.132.53.30']
['185.132.53.30']
['185.132.53.30']必要输出
['51.15.64.60']
['149.3.170.181']
['89.248.166.183']
['185.132.53.30']发布于 2020-09-10 23:02:42
在添加之前,只需检查item (列表)是否已经存在于list2中。
这个答案假设您希望list2没有重复的对象,但保持插入的顺序。
(如果您希望list2没有连续的重复对象,或者如果插入的顺序不重要,则会有不同的答案。)
...
if item != []:
if item not in list2: # Add this line
list2.append (item)
print(list2)发布于 2020-09-10 23:09:54
使用if语句查看项目是否已存在于list2中
下面是对你有效的方法..
import json
import requests
import re
tag = "tag=mirai"
hd = {"Content-Type": "application/x-www-form-urlencoded"}
r = requests.post ('https://urlhaus-api.abuse.ch/v1/tag/', data=tag, headers=hd)
c = ['urls', 'url']
data = json.loads (r.text)
for i in range (0, 998):
fff = data['urls'][i]['url']
pattern = r"((([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])[ (\[]?(\.|dot)[ )\]]?){3}([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]))"
pattern2 = r"^(\S++).*\R(?=(?>.*\R)*?\1 )"
ips = [match[0] for match in re.findall (pattern, fff)]
list2 = []
for item in ips:
if item != []:
if item not in list2:
list2.append (item)
print(list2)https://stackoverflow.com/questions/63832430
复制相似问题