要解决BeautifulSoup href返回空字符串的问题,你可以尝试以下方法:
from bs4 import BeautifulSoup
import requests
url = 'http://example.com'
response = requests.get(url)
html_content = response.text
soup = BeautifulSoup(html_content, 'html.parser')
element = soup.find('a', href=True)
if element:
href_value = element['href']
else:
href_value = ''
完整代码示例:
from bs4 import BeautifulSoup
import requests
url = 'http://example.com'
response = requests.get(url)
html_content = response.text
soup = BeautifulSoup(html_content, 'html.parser')
element = soup.find('a', href=True)
if element:
href_value = element['href']
else:
href_value = ''
print(href_value)
这样你就能得到正确的href值,如果没有找到对应的元素,href_value将会是空字符串。