【发布时间】: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