【数据采集】使用scrapy采集天气网、豆瓣数据信息_基于scrpy爬虫的天气数据采集

image-20211027110644257

1.2.4 数据保存 (多线程)
  • 引入多进程模块
import threading
# 多线程
def download\_imgs(imgList,limit):
    threads = []
    T = [
        threading.Thread(target = download, args=(url,i))
        for i, url in enumerate(imgList[:limit + 1])
    ]
    for t in T:
        t.start()
        threads.append(t)
    return threads

  • 编写下载函数
def download(img_url,name):
    resp = requests.get(img_url)
    try:
        resp = requests.get(img_url)
        with open(f'./images/{name}.jpg', 'wb') as f:
                f.write(resp.content)
    except Exception as e:
        print(f"下载失败: {name} {img\_url} -> {e}")
    else:
        print(f"下载完成: {name} {img\_url}")

就很随机

image-20211027110955637

实验 2

2.1 题目

使用scrapy框架复现作业①

2.2 思路

2.2.1 setting.py
  • 解除限制
ROBOTSTXT_OBEY = False

  • 设置保存图片的路径
IMAGES_STORE = r'.\images'  # 保存文件的路径

  • 打开pipelines
ITEM_PIPELINES = {    
'weatherSpider.pipelines.WeatherspiderPipeline': 300,
}

  • 设置请求头
DEFAULT_REQUEST_HEADERS = {    
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,\*/\*;q=0.8',    
'Accept-Language': 'en',    
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.16 Safari/537.36',
}

2.2.2 item.py
  • 设置要爬取的字段
class WeatherspiderItem(scrapy.Item):    
numbe
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值