我正在使用藝術代碼(如果這是可能的?),我試圖檢索第三組使用findall
正則表達式。我讀了findall
的官方文檔,發現它返回的元組有點欠缺,我想傳遞一個返回第三組的標誌,而不是3個組的元組(前兩個是佔位符)。什麼是最有效的方法來鏈接返回唯一的名字(第三組)而不是後來迭代?如何使用標誌來返回只有一組使用findall-Python
import re, requests
rgx = r"([<][TDtd][>])|(target[=]new[>])(?P<the_deceased>[A-Z].*?)[,]"
urls = {2013: "http://www.killedbypolice.net/kbp2013.html",
2014: "http://www.killedbypolice.net/kbp2014.html",
2015: "http://www.killedbypolice.net/" }
names_of_the_dead = []
for url in urls.values():
response = requests.get(url)
content = response.content
people_killed_by_police_that_year_alone = re.findall(rgx, content)
for dead_person in people_killed_by_police_that_year_alone:
names_of_the_dead.append(dead_person)
#dead_americans_as_string = ",".join(names_of_the_dead)
#print("RIP, {} since 2013:\n".format(len(names_of_the_dead)))
#print(dead_americans_as_string)
In [67]: names_of_the_dead
Out[67]:
[('', 'target=new>', 'May 1st - Dec 31st'),
('', 'target=new>', 'Ricky Junior Toney'),
('', 'target=new>', 'William Jackson'),
('', 'target=new>', 'Bethany Lytle'),
('', 'target=new>', 'Christopher George'),
看起來不錯,還是讓我來試試吧阿維納什 – codyc4321