我正在关注如何使用 Python 制作 Web 抓取应用程序的本教程。具体来说,我正在尝试从我经常访问的购物网站上抓取搜索结果。问题是,当我插入这个网站时,它返回一个错误!
到目前为止的整个功能如下:
from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup
def simple_get(url):
try:
with closing(get(url, stream=True)) as resp:
if is_good_response(resp):
return resp.content
else:
return None
except RequestException as e:
log_error('Error during requests to {0} : {1}'.format(url, str(e)))
return None
def is_good_response(resp):
content_type = resp.headers['Content-Type'].lower()
return (resp.status_code == 200
and content_type is not None
and content_type.find('html') > -1)
def log_error(e):
print(e)
为了测试它,我只有线条
raw_html = simple_get('https://stackoverflow.com/')
print len(raw_html)
它成功地打印了我输入的每个网站的长度,除了我正在为其构建应用程序的网站!当我输入那个网址时,我得到了错误
TypeError: object of type 'NoneType' has no len()
让我头疼的网址是
https://www.mercari.com/search/?facets=2&itemStatuses=1&keyword=loungefly&length=30&sortBy=2