爬取QQ音乐榜单只能爬取到第一首歌
爬取QQ音乐榜单只能爬取到第一首歌,后面的获取不了,求大神解答代码
import urllib.request
import re
def gethtml():
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0'}
req = urllib.request.Request('https://y.qq.com/n/ryqq/toplist/26',headers=header)
html = urllib.request.urlopen(req)
f = html.read()
f = f.decode('utf-8')
return f
def getmusic():
a = r'<a title="([^"]+)" href='
b = gethtml()
muistlis = re.findall(a,b)
for each in muistlis:
return each
c = getmusic()
print(c)
本帖最后由 isdkz 于 2022-4-20 19:30 编辑
import urllib.request
import re
def gethtml():
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0'}
req = urllib.request.Request('https://y.qq.com/n/ryqq/toplist/26',headers=header)
html = urllib.request.urlopen(req)
f = html.read()
f = f.decode('utf-8')
return f
def getmusic():
a = r'<a title="([^"]+)" href='
b = gethtml()
muistlis = re.findall(a,b)
return muistlis # 直接把列表返回
# 不要下面的代码
'''
for each in muistlis:
return each # return会直接退出函数
'''
c = getmusic()
for i in c:
print(i) isdkz 发表于 2022-4-20 19:28
十分感谢大佬,可以加下你的QQ 或者微信吗? bhunht 发表于 2022-4-20 20:23
十分感谢大佬,可以加下你的QQ 或者微信吗?
isdkz_
页:
[1]