簡體   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