【问题标题】:Why my simple python web crawler runs very slowly?为什么我的简单 python 网络爬虫运行很慢?
【发布时间】:2020-07-31 10:35:20
【问题描述】:

我正在尝试抓取大约 34,000 页。我计算了时间,发现请求每个页面平均需要超过 5 秒。由于我是直接从 API 中抓取数据,所以我只使用了 requests 包。有什么办法可以加快我的爬虫速度吗?或者如果不可能,我该如何将爬虫部署到服务器上?

这是我的一些代码:

# Using python selenium to scrape sellers on shopee.co.id
# Crawl one seller -> Crawl all sellers in the list
# Sample URL: https://shopee.co.id/shop/38281755/search
# Sample API: https://shopee.co.id/api/v2/shop/get?shopid=38281755
import pandas as pd
import requests
import json
from datetime import datetime
import time

PATH_1 = '/Users/lixiangyi/FirstIntern/temp/seller_list.csv'
shop_list = pd.read_csv(PATH_1)
shop_ids = shop_list['shop'].tolist()
# print(seller_list)

# Downloading all APIs of shopee sellers:
api_links = []  # APIs of shops
item_links = []  # Links to click into
for shop_id in shop_ids:
    api_links.append('https://shopee.co.id/api/v2/shop/get?shopid=' + str(shop_id))
    item_links.append(
        f'https://shopee.co.id/api/v2/search_items/?by=pop&limit=10&match_id={shop_id}&newest=0&order=desc&page_type=shop&version=2'
    )
# print(api_links)


shop_names = []
shopid_list = []
founded_time = []
descriptions = []
i = 1

for api_link in api_links[0:100]:
    start_time = time.time()
    shop_info = requests.get(api_link)
    shopid_list.append(shop_info.text)
    print(i)
    i += 1
    end_time = time.time()
    print(end_time - start_time)

【问题讨论】:

  • 可能是 API 有速率限制,在这种情况下,您可能无能为力(除了付费,如果他们提供更高质量的付费服务)。虽然它可能有助于找到一种通过更少的 API 调用获取所需信息的方法。
  • 可以提高你的抓取速度。但是,由于您的目标是一个站点,因此提高您的速度可能会导致您的 IP 地址被阻止,这是理所当然的。有些抓取速度非常快,以至于您实际上是在对目标执行拒绝服务攻击,从而损害所有者和其他用户的利益。

标签: python python-requests web-crawler


【解决方案1】:

您应该尝试使用线程或aiohttp 包并行检索多个 URL。使用线程:

更新

由于您的所有请求都针对同一个网站,因此使用requests.Session 对象进行检索会更有效。但是,无论您如何检索这些 URL,在短时间内从同一 IP 地址向同一网站发出过多请求都可能被解释为拒绝服务攻击。

import requests
from concurrent.futures import ThreadPoolExecutor
from functools import partial
import time

api_links = [] # this will have been filled in
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'}

shopid_list = []

def retrieve_url(session, url):
    shop_info = session.get(url)
    return shop_info.text


NUM_THREADS = 75 # experiment with this value
with ThreadPoolExecutor(max_workers=NUM_THREADS) as executor:
    with requests.Session() as session:
        session.headers = headers
        # session will be the first argument to retrieve_url:
        worker = partial(retrieve_url, session)
        start_time = time.time()
        for result in executor.map(worker, api_links):
            shopid_list.append(result)
        end_time = time.time()
        print(end_time - start_time)

【讨论】:

    【解决方案2】:

    使用pythonurllib

    import urllib.request 
    request_url = urllib.request.urlopen(some-url) 
    print(request_url.read()) 
    

    【讨论】:

    • 我发现requests 的运行速度和 Python 自己的文档一样快。
    猜你喜欢
    • 2017-01-26
    • 2016-06-23
    • 1970-01-01
    • 2023-03-13
    • 2021-12-06
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多