IndexError:返回变量时,列表索引超出范围



我刚刚开始/学习使用谷歌云平台(尤其是函数(,我使用BeautifulSoup编写了一个简单的python scraper,它返回了一个错误,我不知道为什么。

from bs4 import BeautifulSoup
import requests
def hello_world(request):
"""Responds to any HTTP request.
Args:
request (flask.Request): HTTP request object.
Returns:
The response text or any set of values that can be turned into a
Response object using
`make_response <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>`.
"""
url = 'https://example.com/'
req = requests.get(url, headers = {'User-Agent': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'})
html = req.text
soup = BeautifulSoup(html, 'html.parser')
title = soup.title
print(title)
return title

当我打印被刮页的标题时,它会很好地显示在日志中。当I返回变量时,日志报告一个";IndexError:列表索引超出范围;。当我返回soup.prettify()时,它也可以正常工作。

这是我在GCP日志中得到的回溯

Traceback(最近调用last(:文件/layers/google.python.pip/pip/lib/python3.9/site packages/sflak/app.py";,第2447行,在wsgi_app response=self.full_dispatch_request((File"/layers/google.python.pip/pip/lib/python3.9/site packages/sflak/app.py";,第1953行,在full_dispatch_request return self.finalize_request(rv(File"/layers/google.python.pip/pip/lib/python3.9/site packages/sflak/app.py";,第1968行,在finalize_request-response=self.make_response(rv(File"中/layers/google.python.pip/pip/lib/python3.9/site packages/sflak/app.py";,行2117,在make_response rv=self.response_class.force_type(rv,request.environment(File"/layers/google.python.pip/pip/lib/python3.9/site packages/werkzeug/wwrappers/base_response.py";,第269行,在force_type response=BaseResponse(*_run_wsgi_app(response,environ((File"中/layers/google.python.pip/pip/lib/python3.9/site packages/werkzeug/wwrappers/base_response.py";,第26行,在_run_wsgi_app中返回_run_wsgi_app(*args(文件"layers/google.python.pip/pip/lib/python3.9/site packages/werkzeug/test.py";,第1123行,在run_wsgi_app中返回app_iter,response[0],Headers(response[1](IndexError:列出索引超出范围

问题可能是由错误的缩进引起的。

顺便说一下,试试这个代码,也许它更容易隐藏:

from bs4 import BeautifulSoup
import requests
url = 'https://stackoverflow.com'
def titleScaper(url):
req = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"})
soup = BeautifulSoup(req.content, 'html.parser')
soup.encode('utf-8') 
return soup.title.get_text()
title = titleScaper(url)
print(title)

相关内容

  • 没有找到相关文章

最新更新