Python: 使用JSON API link 通过正则表达式显示命名的捕获组
Python: Using JSON API link to display named capture group through regex
我在使用正则表达式显示正确命名的捕获组时遇到问题。我已经有了正则表达式公式来捕获该组。这是我的 regex link 展示。通过查看 link,我试图显示以绿色突出显示的文本。
绿色部分是link-containedJSONAPI中的页面标题。它们被标记为 'article.' 到目前为止,我所做的是解析 JSON 以获取文章列表并显示它。有些文章有多个页面,我只是想显示第一页。这就是我使用 REGEX 的原因,因为我在这里处理大文件。我试图让正则表达式的绿色部分显示在我的函数中。这是我没有正则表达式实现的工作代码所在的 link。到目前为止,这是我尝试过的代码:
import json
import requests
import re
link = "https://wikimedia.org/api/rest_v1/metrics/pageviews/top/en.wikiversity/all-access/2018/01/10"
def making_data(link):
response = requests.get(link, [])
data = response.json()
json_data = data['items']
articles_list = []
whole_re= re.compile(r'^[^\/].*')
rx = re.compile(r'(^[^\/]+)')
for items in json_data:
articles = items['articles']
#Iterate over the list of articles
for article in articles:
m = whole_re.match(article)
if m:
articles_list.append(m)
articles = article.get("article")
search_match = rx.match(article)
if search_match:
print("Page: %s" % articles)
return sorted(articles_list)
making_data(link)
我一直收到正则表达式错误。我想我用 JSON 和正则表达式实现这个错误。
我希望输出仅显示提供的正则表达式 link 中以绿色突出显示的内容,而不是之后的以下文本。
Page: Psycholinguistics
Page: Java_Tutorial
Page: United_States_currency
我希望这一切都有意义。我感谢所有的帮助。
如果您打印 article
,您会看到它是字典格式。您的正则表达式在这里没有问题,而是您引用 article
.
的方式
我相信您打算从您链接的原始代码中引用 article_title = article.get("article")
。
另一件会成为问题的事情是在循环中间重命名 articles
。我为您进行了一些编辑,但需要根据您的确切用法和您想要的结果进行一些改进。
您可以使用 .group(1)
引用匹配对象组
import json
import requests
import re
link = "https://wikimedia.org/api/rest_v1/metrics/pageviews/top/en.wikiversity/all-access/2018/01/10"
def making_data(link):
response = requests.get(link, [])
data = response.json()
json_data = data['items']
articles_list = []
whole_re= re.compile(r'^[^\/].*')
rx = re.compile(r'(^[^\/]+)')
for items in json_data:
articles = items['articles']
#Iterate over the list of articles
for article in articles:
article_title = article.get("article")
m = whole_re.match(article_title)
if m:
articles_list.append(m[0])
search_match = rx.match(article_title)
if search_match:
print("Page: %s" % search_match.group(1))
return sorted(articles_list)
making_data(link)
我在使用正则表达式显示正确命名的捕获组时遇到问题。我已经有了正则表达式公式来捕获该组。这是我的 regex link 展示。通过查看 link,我试图显示以绿色突出显示的文本。
绿色部分是link-containedJSONAPI中的页面标题。它们被标记为 'article.' 到目前为止,我所做的是解析 JSON 以获取文章列表并显示它。有些文章有多个页面,我只是想显示第一页。这就是我使用 REGEX 的原因,因为我在这里处理大文件。我试图让正则表达式的绿色部分显示在我的函数中。这是我没有正则表达式实现的工作代码所在的 link。到目前为止,这是我尝试过的代码:
import json
import requests
import re
link = "https://wikimedia.org/api/rest_v1/metrics/pageviews/top/en.wikiversity/all-access/2018/01/10"
def making_data(link):
response = requests.get(link, [])
data = response.json()
json_data = data['items']
articles_list = []
whole_re= re.compile(r'^[^\/].*')
rx = re.compile(r'(^[^\/]+)')
for items in json_data:
articles = items['articles']
#Iterate over the list of articles
for article in articles:
m = whole_re.match(article)
if m:
articles_list.append(m)
articles = article.get("article")
search_match = rx.match(article)
if search_match:
print("Page: %s" % articles)
return sorted(articles_list)
making_data(link)
我一直收到正则表达式错误。我想我用 JSON 和正则表达式实现这个错误。
我希望输出仅显示提供的正则表达式 link 中以绿色突出显示的内容,而不是之后的以下文本。
Page: Psycholinguistics
Page: Java_Tutorial
Page: United_States_currency
我希望这一切都有意义。我感谢所有的帮助。
如果您打印 article
,您会看到它是字典格式。您的正则表达式在这里没有问题,而是您引用 article
.
我相信您打算从您链接的原始代码中引用 article_title = article.get("article")
。
另一件会成为问题的事情是在循环中间重命名 articles
。我为您进行了一些编辑,但需要根据您的确切用法和您想要的结果进行一些改进。
您可以使用 .group(1)
import json
import requests
import re
link = "https://wikimedia.org/api/rest_v1/metrics/pageviews/top/en.wikiversity/all-access/2018/01/10"
def making_data(link):
response = requests.get(link, [])
data = response.json()
json_data = data['items']
articles_list = []
whole_re= re.compile(r'^[^\/].*')
rx = re.compile(r'(^[^\/]+)')
for items in json_data:
articles = items['articles']
#Iterate over the list of articles
for article in articles:
article_title = article.get("article")
m = whole_re.match(article_title)
if m:
articles_list.append(m[0])
search_match = rx.match(article_title)
if search_match:
print("Page: %s" % search_match.group(1))
return sorted(articles_list)
making_data(link)