shell爬虫
#!/bin/bash
curl_str='curl -x "http://http-pro.abuyun.com:9010" --proxy-basic --proxy-user H78H42TCN191075P:3D1EA6E4F458AB69'
curl_str='curl -L --socks5 socks-cla.abuyun.com:8030 --proxy-user S822RB9T27K96TPC:5E68523C79E62C41'
### encode url
encode_url(){
local supplier_name_encode="$(echo "$" | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g')"
echo "https://xin.baidu.com/s?q=${supplier_name_encode}&t=0"|perl -npe 's/\n//'
} do_down_web_info_html(){
local url=$(encode_url "$1")
local html_file="$2"
# wget ${url} -O ${html_file}
#echo -e "\n===================================> " ${curl_str} "${url}" \> ${html_file}"\n"
${curl_str} "${url}" > ${html_file}
} get_supplier_pid(){
local html_file="${1}.html"
local supplier_name="$2"
do_down_web_info_html "${supplier_name}" "${html_file}"
### 获取pid
echo $(grep compinfo ${html_file}|head -|awk -F'pid=' '{print $2}'|awk -F'"' '{print $1}')
} do_down_supplier_unifiedcode(){
local id="$1"
local pid="$2"
### 获取统一社会信用代码
local unifiedCode="${id}_unifiedCode"
local api_basic_url="https://xin.baidu.com/detail/basicAjax?pid=${pid}"
echo -e "\n 获取统一社会信用代码 ===================================> "${curl_str} "${api_basic_url}" \> ${unifiedCode}"\n"
${curl_str} "${api_basic_url}" > ${unifiedCode}
} do_switch_ip(){
# ${curl_str} http://proxy.abuyun.com/switch-ip
echo "do_switch_ip"
} do_run(){
local id="$1"
local supplier_name="$2"
pid=$(get_supplier_pid "${id}" "${supplier_name}")
do_down_supplier_unifiedcode "${id}" "${pid}"
# echo -n $(date "+%F %T")" | ${id} | ${supplier_name} | ${pid} |"
### 解析json为csv文件
# jq -r '[(.data.entName|tostring),(.data.unifiedCode|tostring)]|join("|")' "${id}_unifiedCode"
} result_file="result_code"
# "上海东福网络科技有限公司广州分公司","" token(){
local pid=$
# 判断是否有传入pid
if [ -z "${pid}" ]
then
echo "please input pid"
exit
fi # 设置并发数,默认为5
local concurrency=
if [ -n "$2" ]
then
concurrency=$
echo "Concurrency: $2"
fi # 创建有名管道,如果fd1不存在则创建
[ -e /tmp/fd1 ] || mkfifo /tmp/fd1
# 创建文件描述符,以可读(<)可写(>)的方式关联管道文件,这时候文件描述符999就有了有名管道文件的所有特性
# 为了让程序有一定的扩展性,不想写死fd,因而引入了变量。
# 因而引入eval命令,强制shell进行变量展开。
# eval exec "${fd}>file"简单的说,eval将右边参数整体作为一个命令,进行变量的替换,然后将替换后的输出结果给shell去执行。
eval exec "${pid}<>/tmp/fd1"
# 关联后的文件描述符拥有管道文件的所有特性,所以这时候管道文件可以删除,我们留下文件描述符来用就可以了
[ -e /tmp/fd1 ] && rm -f /tmp/fd1 # 初始化并行数
for ((i=;i<=${concurrency};i++))
do
# &999代表引用文件描述符999,这条命令代表往管道里面放入了一个"令牌"
echo ${i}>&${pid}
done
} main(){
local pid=$$
local start_time=`date +%s`
# 生成管道文件
token ${pid}
local num=
cat ../tmpa|head -|tail -|while read line
do
num=$((${num}+))
if [ ${num} -eq ]
then
do_switch_ip
num=
echo "==================================== reset num ===================================="
fi
# 获取令牌
read -u${pid} name
{
local supplier_info=($(echo ${line}|sed 's/"//g'|sed 's/,/ /'))
local id="${supplier_info[1]}"
local supplier_name="${supplier_info[0]}"
echo "${id} | ${supplier_name}"
do_run ${id} ${supplier_name}
# | tee -a ${result_file}_${name}
echo ${name}>&${pid}
} &
done wait
# 定义脚本运行的结束时间
local stop_time=`date +%s`
echo "TIME:`expr ${stop_time} - ${start_time}`"
# 关闭文件描述符的读
eval exec "${pid}<&-"
# 关闭文件描述符的写
eval exec "${pid}>&-"
} main
shell爬虫的更多相关文章
- Linux shell爬虫实现树洞网自动回复Robot
奇怪的赞数 人生在世,不如意事十之八九,可与言者无二三人.幸好我们生在互联网时代,现实中找不到可以倾诉的人还可以在网络上寻找发情绪宣泄口,树洞这类产品就是提供一个让人在网络上匿名倾诉的平台. 我是偶然 ...
- shell爬虫--抓取某在线文档所有页面
在线教程一般像流水线一样,页面有上一页下一页的按钮,因此,可以利用shell写一个爬虫读取下一页链接地址,配合wget将教程所有内容抓取. 以postgresql中文网为例.下面是实例代码 #!/bi ...
- shell爬虫简易脚本(线程数可控)
1.介绍 以机电之家网站为例 经过初步分析,机电之家的数据量较大,并且数据组织规则较为统一,适合以代码方式进行全量爬取. 企业列表URL统一为http://www.jdzj.com/yp_vlist_ ...
- shell 爬虫 从日志分析到数据采集与分析
[root@VM_61_158_centos ~]# curl http://ip.chinaz.com/220.112.233.179 |grep -e Whwtdhalf.*span.*span. ...
- shell及Python爬虫实例展示
1.shell爬虫实例: [root@db01 ~]# vim pa.sh #!/bin/bash www_link=http://www.cnblogs.com/clsn/default.html? ...
- scrapy知识积累
Scrapy 中文文档https://scrapy-chs.readthedocs.io/zh_CN/latest/intro/overview.html 创建项目 scrapy startproje ...
- SHELL网络爬虫实例剖析--转载
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://nolinux.blog.51cto.com/4824967/1552472 前天 ...
- 爬虫系列3:scrapy技术进阶(xpath、rules、shell等)
本文主要介绍与scrapy应用紧密相关的关键技术,不求很深入,但求能够提取要点.内容包括: 1.xpath选择器:选择页面中想要的内容 2.rules规则:定义爬虫要爬取的域 3.scrapy she ...
- Python爬虫教程-33-scrapy shell 的使用
本篇详细介绍 scrapy shell 的使用,也介绍了使用 xpath 进行精确查找 Python爬虫教程-33-scrapy shell 的使用 scrapy shell 的使用 条件:我们需要先 ...
随机推荐
- mongodb本地搭建过程
1.解压安装包后安装 安装时注意:1.选择customs 2.路径选择C盘以外的盘符 安装完成后: 2.在bin的同级目录下新建data.log文件夹 3.在data文件夹下新建db文件夹,在l ...
- 关于parseInt进行进制的转换
["1", "2", "3"].map(parseInt) 答案是多少? 考察点:1 . ES5的map方法,接收参数并且callback计 ...
- Hackerrank--Mixing proteins(Math)
题目链接 Some scientists are working on protein recombination, and during their research, they have foun ...
- HTML 和 XHTML 区别
1.初级改善 为页面添加正确的DOCTYPE 很多设计师和开发者都不知道什么是DOCTYPE,DOCTYPE有什么用. DOCTYPE是document type的简写.主要用来说明你用的XHTML或 ...
- python中os模块简介
一.什么是os模块 os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,根据不同的平台进行相应的操作,在python编程时,经常和文件.目录打交道,所以离不了 ...
- 学习Python笔记---操作列表
1.for循环: 编写for循环时,对于用语存储列表中每个值的临时变量,可指定任何名称. 在for循环中,想包含多少行代码都可以,每个缩进的代码行都是循环的一部分,且将针对列表中的每个值都执行一次. ...
- JSP-request(httpServletRequest)
HttpServletRequest 1 HttpServletRequest概述 2 request运行流程 3 通过抓包工具抓的http请求 4 请求行信息的相关方法 //1.获得请求方式 Str ...
- Chrome谷歌浏览器调试
Chrome浏览器调试技巧 https://blog.csdn.net/u014727260/article/details/53231298
- mybatis官网文档mybatis_doc
在平时的学习中,我们可以去参考官网的文档来学习,这个文档有中文的,方便我们去阅读,而且这里的分类很详细. 官网文档链接:http://www.mybatis.org/mybatis-3/zh/inde ...
- 安装tengine及淘宝会话保持模块
安装tengine及淘宝会话保持模块 下载http://tengine.taobao.org/ 解压tar -zxvf tengine-2.3.0.tar.gz 安装GCC: yum -y insta ...