繁体   English   中英

在python request.get()方法中出现错误

[英]getting error in python requests.get() method

import string
import requests
from bs4 import BeautifulSoup

song_name = input('enter song name : ')
url = 'https://search.azlyrics.com/search.php?q='  + (string.capwords(song_name)).replace(' ', '+')
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')

for a in soup.find_all('a'):
    href = a.get('href')
    if href.find('https') != -1:
        if href.find('lyrics/') != -1:
            print(href)
            res = requests.get(href)
            break

当我将href参数传递到requests.get(href)它将引发错误。 而当我传递常量字符串时,例如res = requests.get('https://google.co.in')它不会引发任何错误。

我检查了两个变量的类型是否相同

print(type(href))
<class 'str'>

print(type('https://google.co.in'))
<class 'str'>

两者都是同一种类型,那么为什么我会出错。

输入: 输入歌曲名称:神的计划

您的href可能不是完全合格的URL。

您可以检查自己的href是否以“ http”或“ www”开头。

如果不是,则可能是#foofoo/bar.html的形式,在这种情况下,您可以将其简单地附加到当前页面URL的末尾

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM