|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
爬取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)
复制代码
|
|