Skip to content

Commit 331b218

Browse files
authored
Update 1.py
移除过滤器,检查是否能正常抓取。 使用默认线程数,避免因为线程数设置不当导致资源分配问题。 调试模式,增加日志输出,以便更容易调试问题。
1 parent c6c120c commit 331b218

File tree

1 file changed

+23
-21
lines changed
  • 第七章/7.4 批量内容爬取

1 file changed

+23
-21
lines changed

第七章/7.4 批量内容爬取/1.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
# coding=gbk
22
"""
3-
作者:川川
4-
@时间 : 2023/12/9 0:44
3+
作者:川川
4+
@时间 : 202/02/18 0:44
55
"""
66
# pip install beautifulsoup4 icrawler
7+
import os
78
from icrawler.builtin import BingImageCrawler
89

9-
# 需要爬取的关键字
10-
list_word = ['宝马车','奔驰车']
10+
# 需要爬取的关键字
11+
list_word = ['比亚迪汽车']
1112

12-
filters = dict(
13-
size='large',
14-
color='color',
15-
license='commercial,modify',
16-
date='pastyear'
17-
)
13+
# 确保路径存在
14+
if not os.path.exists('photo'):
15+
os.makedirs('photo')
1816

1917
for word in list_word:
20-
# bing爬虫
21-
# 保存路径
22-
bing_storage = {'root_dir': 'photo\\' + word} # photo为主文件名
23-
# 从上到下依次是解析器线程数,下载线程数,还有上面设置的保存路径
24-
bing_crawler = BingImageCrawler(parser_threads=4,
25-
downloader_threads=8,
26-
storage=bing_storage)
27-
# 开始爬虫,关键字+图片数量
28-
bing_crawler.crawl(keyword=word,
29-
filters=filters,
30-
max_num=10)
18+
# 保存路径
19+
bing_storage = {'root_dir': os.path.join('photo', word)} # 使用os.path.join处理路径
20+
21+
# 创建BingImageCrawler实例
22+
bing_crawler = BingImageCrawler(
23+
parser_threads=3, # 使用3个解析线程
24+
downloader_threads=4, # 使用4个下载线程
25+
storage=bing_storage
26+
)
27+
28+
# 开始爬虫,关键字+图片数量
29+
bing_crawler.crawl(
30+
keyword=word, # 关键字
31+
max_num=10 # 最大下载10张图片
32+
)

0 commit comments

Comments
 (0)