0

我正在关注如何使用 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
4

1 回答 1

0

我建议您可以更轻松地获得一个模板。请将您的路径指向 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()
于 2020-05-02T12:40:55.980 回答