python爬取主播信息

2023-06-07,,

之前学过python的爬虫技术,现在回顾一下看看还会不会,果然有坑。

爬取了微博评论网友的id
代码如下

import requests
url = 'https://m.weibo.cn/api/comments/show?id=4188633986790962&page=6
h = requests.get(url)
print(h.json()['data']['data'][0]['user']['id'])

执行的时候报错
Traceback (most recent call last):
File "e:/personal/vscode/pameinv.py", line 9, in <module>
print(a())
File "e:/personal/vscode/pameinv.py", line 8, in a
return html.json()
File "D:\python\Python37\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "D:\python\Python37\lib\json__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "D:\python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
于是开始各种调试 最后换了一个地址行了,可能是微博的api不让调用了
于是修改了一下程序

import requests
url = 'http://www.yy.com/api/yyue-spot-news'
h = requests.get(url)
for j in range(len(h.json()['data'])):
print(h.json()['data'][j]['id']) #这里有缩进

print里的内容是根据网页里的相应信息而定的

最后运行了一下 成功显示了主播的id

《python爬取主播信息.doc》

下载本文的Word格式文档,以方便收藏与打印。