我是按照 本教程 关于如何用Python做一个网络搜刮应用。具体来说,我想从我常去的一个购物网站上搜刮搜索结果。问题是,当我插入这个网站时,它返回的是一个错误!
到目前为止,整个功能如下。
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
我建议你用一个模板,你可以更容易得到.请把你的路径变成Chrome驱动的位置。
import os
from selenium import webdriver
chromedriver = "/Users/your_account/Downloads/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
url = 'https://www.mercari.com/search/?facets=2&itemStatuses=1&keyword=loungefly&length=30&sortBy=2'
driver.get(url)
# driver.quit()