点击上方 "程序员小乐"关注公众号, 星标或置顶一起成长
每天早上8点20分, 第一时间与你相约
每日英文
It has not been the time yet to give up as long as you still feel it is not the end. I could cry for a thousand times if you promise me a happy ending.
只要心里还存着不甘心,就还不到放弃的时候。只要是喜剧收尾,过程你让我怎么哭都行。
每日掏心话
不是每一次努力都会有收获,但是,每一次收获都必须努力,这是一个不公平的不可逆转的命题。
来自:张永清 | 责编:乐乐
链接:cnblogs.com/laoqing
程序员小乐(ID:study_tech)第 639 次推文 图片来自网络
往日回顾:分享技术人学习有用的国外网站
正文
首先来看一下一个爬虫平台的设计,作为一个爬虫平台,需要支撑多种不同的爬虫方式,所以一般爬虫平台需要包括:
pip install scrapy
scrapy startproject zj_scrapy
cd zj_scrapy
scrapy genspider sjqq “sj.qq.com”
# -*- coding: utf-8 -*-
# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html
import scrapy
class ZjScrapyItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
name = scrapy.Field()
pass
from scrapy.http import HtmlResponse
from zj_scrapy.items import ZjScrapyItem
class SjqqSpider(scrapy.Spider):
name = 'sjqq'
allowed_domains = ['sj.qq.com']
start_urls = ['https://sj.qq.com/myapp/category.htm?orgame=1&categoryId=114']
def parse(self, response:HtmlResponse):
name_list = response.xpath('/html/body/div[3]/div[2]/ul/li')
print("=============",response.headers)
for each in name_list:
item = ZjScrapyItem()
name = each.xpath('./div/div/a[1]/text()').extract()
item['name'] = name[0]
yield item
# Define your item pipelines here
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
class ZjScrapyPipeline(object):
def process_item(self, item, spider):
print("+++++++++++++++++++",item['name'])
print("-------------------",spider.cc)
return item
ITEM_PIPELINES = {
'zj_scrapy.pipelines.ZjScrapyPipeline': 300,
}
FEED_EXPORT_ENCODING = 'utf-8'
# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 32
DEFAULT_REQUEST_HEADERS = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en',
pip install scrapyd
pip install scrapyd-client
scrapyd-deploy <target> -p <project> --version <version>
curl http://localhost:6800/schedule.json -d project= zj_scrapy -d spider=sjqq
{
"node_name": "ZJPH-0321",
"status": "ok",
"jobid": "dd7f10aca76e11e99b656c4b90156b7e"
project (string, required) –项目名
version (string, required) –项目版本,不填写则是当前时间戳
egg (file, required) –当前项目的egg文件
curl http://localhost:6800/addversion.json -F project=myproject -F version=r23 -F egg=@myproject.egg
job (string, required) -jobid
curl http://localhost:6800/cancel.json -d project=myproject -d job=6487ec79947edab326d6db28a2d86511e8247444
curl http://localhost:6800/listprojects.json
curl http://localhost:6800/listversions.json?project=myproject
_version (string, optional) –版本号
$ curl http://localhost:6800/listspiders.json?project=myproject
project (string, option) - restrict results to project name
curl http://localhost:6800/listjobs.json?project=myproject | python -m json.tool
project (string, required) - the project name
version (string, required) - the project version
curl http://localhost:6800/delversion.json -d project=myproject -d version=r99
curl http://localhost:6800/delproject.json -d project=myproject
# -*- coding: utf-8 -*-import requestsfrom bs4 import BeautifulSoupimport timeclass SyncCrawlSjqq(object): def parser(self,url): req = requests.get(url) soup = BeautifulSoup(req.text,"lxml") name_list = soup.find(class_='app-list clearfix')('li') names=[]for name in name_list: app_name = name.find('a',class_="name ofh").text names.append(app_name)return namesif __name__ == '__main__': syncCrawlSjqq = SyncCrawlSjqq() t1 = time.time() url = "https://sj.qq.com/myapp/category.htm?orgame=1&categoryId=114"print(syncCrawlSjqq.parser(url)) t2 = time.time()print('一般方法,总共耗时:%s' % (t2 - t1))
import requests
from bs4 import BeautifulSoup
from flask import Flask, request, Response
import json
app = Flask(__name__)
class SyncCrawlSjqq(object):
def parser(self,url):
req = requests.get(url)
soup = BeautifulSoup(req.text,"lxml")
name_list = soup.find(class_='app-list clearfix')('li')
names=[]
for name in name_list:
app_name = name.find('a',class_="name ofh").text
names.append(app_name)
return names
@app.route('/getSyncCrawlSjqqResult',methods = ['GET'])
def getSyncCrawlSjqqResult():
syncCrawlSjqq=SyncCrawlSjqq()
return Response(json.dumps(syncCrawlSjqq.parser(request.args.get("url"))),mimetype="application/json")
if __name__ == '__main__':
app.run(port=3001,host='0.0.0.0',threaded=True)
#app.run(port=3001,host='0.0.0.0',processes=3)
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED
import time
class SyncCrawlSjqqMultiProcessing(object):
url = "https://sj.qq.com/myapp/category.htm?orgame=1&categoryId=114"
executor = ThreadPoolExecutor(max_workers=20)
syncCrawlSjqqMultiProcessing = SyncCrawlSjqqMultiProcessing()
t1 = time.time()
future_tasks=[executor.submit(print(syncCrawlSjqqMultiProcessing.parser(url)))]
wait(future_tasks, return_when=ALL_COMPLETED)
t2 = time.time()
print('一般方法,总共耗时:%s' % (t2 - t1))
欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,学习能力的提升上有新的认识,欢迎转发分享给更多人。
欢迎各位读者加入程序员小乐技术群,在公众号后台回复“加群”或者“学习”即可。
猜你还想看
阿里、腾讯、百度、华为、京东最新面试题汇集
GitHub 标星3.5W+,超实用技术面试手册,从工作申请、面试考题再到优势谈判
漫画:一位“坑人”的编程大师
Redis基础都不会,好意思出去面试?
Java提供的几种线程池
文章有问题?点此查看未经处理的缓存