my code is running fine but everytime iam running it i always get this error
Traceback (most recent call last):
File "pixiv2.py", line 20, in <module>
print(url["url"])
KeyError: 'url
here my code
import requests
import json
head = {"cookie":"first_visit_datetime_pc=2022-10-18 23:14:39;p_ab_id=6; p_ab_id_2=5; p_ab_d_id=733460558; yuid_b=FxYVh2A; _gcl_au=1.1.16757329.1666102482; _fbp=fb.1.1666102483510.1284265535; __utmv=235335808.|2=login ever=no=1^3=plan=normal=1^9=p_ab_id=6=1^10=p_ab_id_2=5=1^11=lang=en=1; _im_vid=01GFNQ5BHBK0BTS863E1PFV5XE; p_b_type=1; PHPSESSID=nno1inm7i9jm7tm3nn65h57fa60juhja; __utma=235335808.40218806.1666102482.1666174342.1666263027.5; __utmc=235335808; __utmz=235335808.1666263027.5.3.utmcsr=bing|utmccn=(organic)|utmcmd=organic|utmctr=(not provided); __utmt=1; __cf_bm=qIXpzNczxkOor5MRFjhtcdyHRIoxMmfx4WjFBgLIaj0-1666263124-0-ATZ81C6AK30YynV2vdZUJy/1mOKr4bQsU6Jg8IQQ3NXN1igC9JmUCbw3UEi3Q94qZFmJGQCCbEiIWh9AkYF5jefgfEbxU/G8cZ90Y2Q9nIIz; _ga=GA1.2.40218806.1666102482; _gid=GA1.2.185213707.1666263130; tag_view_ranking=uusOs0ipBx~jhuUT0OJva~YbOo-qnBCR~BCvXu--kz8~zJbQX9XnS8~lGA3C7DKHJ~HPo9KI2ZWw~OH5ieNjgYI~0im78x6u68~j2Cs25NHKk~C1SJYGypg3~Ie2c51_4Sp~-wJIMuUetf~CZnOKinv48~5oPIfUbtd6~CtrLViGu8y~uXRQFgCSKj~3rrLyfdz97~I9PKKPZAGj~ukPRYeVH48~ovVM8EeCy5~GyDpAhs6Fr~FUI9Gz_VHc~LoDIs84uJh~T7wAIfdFn4~Kvc_vdVxQf~4mvK-PkuW6~shc8mfpHNf~vwW0YMBATT~Il2Wqia8Nd~zx-g5-W1ik~V_9MwDcP_t~nQRrj5c6w_~Oa9b6mEc1T~SvhFpM2bDA~av3fypOM0M~aPRj6MFMA5~jH0uD88V6F~2pZ4K1syEF~pGv7p05oAU~a6iatBgjr5~Nbvc4l7O_x~t21uiB0eUi~Mn6hsE75l9~jfnUZgnpFl~ZISI5v0br2~azESOjmQSV~pqhfTTXWkX~r8wAwzuHSI~o_4V1CUos4~aiBQfF9p1b~OIzjMQQa-D~Sz48L5kEb5~98c-9jH-Jp~sL9NH1EVf3~8iMVyPUkbe~DxtiQJj6de~WDg8bdJiXI~dDVC9t_E1h~qm_vcCNQ6W~3G3U6TElkH~mWmWMybG2e~UXbzwpvQqi~dC_oGmOpIw~cE1cc1WuGz~f3kE4r7g96~AZsiSz9Ta3~HftrmK7zQW~1jjRk-sprI; __utmb=235335808.22.9.1666263045840; _ga_75BBYNYN9J=GS1.1.1666263026.8.1.1666263555.0.0.0; cto_bundle=s5eMA19uJTJGVEtqR2ZEa3FkNUV3NENaWmN4bFRoQ3hzNjJYcXdiTG1sY0RLRUZ1bU04UUlCRWJ1TFNtS3oyS3YwWHExV1Z6SjI5RTNyQUxFU3JES2xtSDdYdUI4VG1va0RSYnZjUGNHWXR2ZCUyRnNKTzdJTzRNczgxJTJGZHhkdEY2T2FISyUyQnlzQzN4JTJGOVc3U29yRWU1eWI1d2JpNm1HM2Q3YXBwRExTMyUyQk55N3Z3eENYVzglM0Q",
"referer": "https://www.pixiv.net/en/",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.47"}
query = "13322496"
data = {"q": query}
link = 'https://www.pixiv.net/ajax/illust/13322496/recommend/init?limit=30&lang=en'
req = requests.get(link, data=data, headers=head)
r = req.json()
for url in r["body"]["illusts"]:
print(url["url"])
CodePudding user response:
How to ignore exceptions in python: Python: How to ignore an exception and proceed?
implemented in your code, but I would also look at @matszwecja's answer to understand why this error is occurring.
for url in r["body"]["illusts"]:
try:
print(url["url"])
except KeyError:
pass
CodePudding user response:
You can use dict.get
,
for url in r['body']['illusts']:
print(url.get('url'))
To filter out the None
values. You can use filter
list(filter(None, [url.get('url') for url in r['body']['illusts']]))