我正在尝试通过以下代码获取电视指南信息。但是我得到TypeError:字符串索引必须是整数。
任何帮助都会非常有用。
import requests
url="https://www.digiturk.com.tr/_Services/TVguide/jProxy.aspx?cid=271&sd=13_4_2020_0_0"
html_content = requests.get(url).text
remove_copy="/*Copyright © 2009 Digital Platform İletişim Hizmetleri A.Ş. Tüm Hakları Saklıdır. Bu servisin izinsiz kullanımından doğacak tüm yasal yükümlülükleri izinsiz kullanan kişiler kabul etmiş sayılır.*/"
page_content=html_content.split(remove_copy)[-1]
null="null"
for ch in f["BChannels"]:
for pr in ch["CPrograms"]:
print(pr["PName"], pr["POName"], pr["BID"], pr["PDuration"])
答案 0 :(得分:0)
page_content=html_content.split(remove_copy)[-1]
page_content
是一个字符串。您必须对其进行解析才能将其用作字典:
import json
...
page_content = json.loads(html_content.split(remove_copy)[-1])
...
for ch in page_content["BChannels"]:
for pr in ch["CPrograms"]:
print(pr["PName"], pr["POName"], pr["BID"], pr["PDuration"])
DERİN SULAR SUBMERGENCE 1048340359 6285
AZ SONRA... 1048340614 622
YEŞİL REHBER GREEN BOOK 1048340360 7458
AZ SONRA... 1048446330 934
AQUAMAN 1048446078 8245
AZ SONRA... 1048446329 1027
EVCİL HAYVANLARIN GİZLİ...2 THE SECRET LIFE OF PETS 2, THE ( 1048446287 4947
AZ SONRA... 1048446328 1056
KINGS Kings 1048446079 4887
AZ SONRA... 1048446327 1149
PARAZİT PARASITE 1048446285 7486
AZ SONRA... 1048446326 1482
HIRSIZLAR KRALI KING OF THIEVES 1048446080 6197
AZ SONRA... 1048446325 1352
VOX LUX 1048446081 6546
AZ SONRA... 1048446331 923
10x10 1048446082 4813
AZ SONRA... 1048446324 594
TULLY 1048446083 5485
AZ SONRA... 1048446323 3526
PARAZİT PARASITE 1048446295 7486
AZ SONRA... 1048446332 313
HIRSIZLAR KRALI KING OF THIEVES 1048446084 6650
答案 1 :(得分:-1)
尝试:
import requests
url="https://www.digiturk.com.tr/_Services/TVguide/jProxy.aspx?cid=271&sd=13_4_2020_0_0"
html_content = requests.get(url).text
remove_copy="/*Copyright © 2009 Digital Platform İletişim Hizmetleri A.Ş. Tüm Hakları Saklıdır. Bu servisin izinsiz kullanımından doğacak tüm yasal yükümlülükleri izinsiz kullanan kişiler kabul etmiş sayılır.*/"
page_content=html_content.split(remove_copy)[-1]
null="null"
f = eval(page_content)
for ch in f["BChannels"]:
for pr in ch["CPrograms"]:
print(pr["PName"], pr["POName"], pr["BID"], pr["PDuration"])
eval()
将字符串转换为字典,以便可以遍历。
代替eval(),还可以使用json库。 (需要使用import json
导入json)
更改:
f = eval(page_content)
收件人:
f = json.loads(page_content)
输出:
DERİN SULAR SUBMERGENCE 1048340359 6285
AZ SONRA... 1048340614 622
YEŞİL REHBER GREEN BOOK 1048340360 7458
AZ SONRA... 1048446330 934
AQUAMAN 1048446078 8245
AZ SONRA... 1048446329 1027
EVCİL HAYVANLARIN GİZLİ...2 THE SECRET LIFE OF PETS 2, THE ( 1048446287 4947
AZ SONRA... 1048446328 1056
KINGS Kings 1048446079 4887
AZ SONRA... 1048446327 1149
PARAZİT PARASITE 1048446285 7486
AZ SONRA... 1048446326 1482
HIRSIZLAR KRALI KING OF THIEVES 1048446080 6197
AZ SONRA... 1048446325 1352
VOX LUX 1048446081 6546
AZ SONRA... 1048446331 923
10x10 1048446082 4813
AZ SONRA... 1048446324 594
TULLY 1048446083 5485
AZ SONRA... 1048446323 3526
PARAZİT PARASITE 1048446295 7486
AZ SONRA... 1048446332 313
HIRSIZLAR KRALI KING OF THIEVES 1048446084 6650