【发布时间】:2020-04-10 08:28:59
【问题描述】:
这样的JSON文件
{"ip":[{
"urllist":{
"url1",
"url2",
"url3"
}
}
]}
我有一个这样的列表。我通过解析 JSON 文件得到了这个列表
urls = [{'url1', 'url2', 'url3',...}]
我无法处理这个问题
for url in urls:
url = requests.get(url)
我添加了完整的代码;我提到了上面的 JSON 文件内容
TEMPLATE: str = """
{}
"""
LAST = dict()
ip_address: List[str] = list()
def crop_list(_list: List[Any]):
global LAST
try:
idx = _list.index(LAST)
return _list[idx:]
except ValueError:
return _list
def parse_ip():
data = crop_list(jsonData["ip"])
for item in data:
txt = item["urllist"]
ip_address.append(TEMPLATE.format(txt))
for url in ip_address:
resp = requests.get(url)
【问题讨论】:
-
你能分享你的错误吗?
-
如果您可以将 JSON 文件解析为变量
tmp,您只需使用:tmp['ip'][0]['urllist']即可获得{'url1', 'url2', 'url3',...},并且您可以按照您的描述使用 for 循环。 -
用嵌套循环试试,你正在遍历 urls(list),url 将是一个集合...所以也遍历集合
-
'raise InvalidSchema("No connection adapters were found for {!r}".format(url)) requests.exceptions.InvalidSchema: No connection adapters were found for' my url list @Gabip跨度>
标签: python list web web-scraping python-requests