python采集百度搜索结果带有特定URL的链接代码实例


Posted in Python onAugust 30, 2019

这篇文章主要介绍了python采集百度搜索结果带有特定URL的链接代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

#coding utf-8
import requests
from bs4 import BeautifulSoup as bs
import re
from Queue import Queue
import threading
from argparse import ArgumentParser

arg = ArgumentParser(description='baidu_url_collet py-script by xiaoye')
arg.add_argument('keyword',help='keyword like inurl:?id=for searching sqli site')
arg.add_argument('-p','--page',help='page count',dest='pagecount',type=int)
arg.add_argument('-t','--thread',help='the thread_count',dest='thread_count',type=int,default=10)
arg.add_argument('-o','--outfile',help='the file save result',dest='oufile',type=int,default='result.txt')
result = arg.parse_args()
headers = {'User-Agent':'Mozilla/5.0(windows NT 10.0 WX64;rv:50.0) Gecko/20100101 Firefox/50.0'}

class Bg_url(threading.Thread):
  def __init__(self,que):
    threading.Thread.__init__(self)
    self._que = que
  def run(self):
    while not self._que.empty():
      URL = self._que.get()
      try:
        self.bd_url_collet(URL)
      except Exception,e:
        print(e)
        pass
  def bd_url_collect(self, url):
    r = requests.get(url, headers=headers, timeout=3)
    soup = bs(r.content, 'lxml', from_encoding='utf-8')
    bqs = soup.find_all(name='a', attrs={‘data-click‘:re.compile(r'.'), 'class':None})#获得从百度搜索出来的a标签的链接
    for bq in bqs:
      r = requests.get(bq['href'], headers=headers, timeout=3)#获取真实链接
      if r.status_code == 200:#如果状态码为200
        print r.url
        with open(result.outfile, 'a') as f:
          f.write(r.url + '\n')
def main():
  thread = []
  thread_count = result.thread_count
  que = Queue()
  for i in range(0,(result.pagecount-1)*10,10):
  que.put('https://www.baidu.com/s?wd=' + result.keyword + '&pn=' + str(i))
  or i in range(thread_count):
  thread.append(Bd_url(que))
  for i in thread:
    i.start()
  for i in thread:
    i.join()    
if __name__ == '__main__':
  main()  
#执行格式
python aaaaa.py "inurl:asp?id=" -p 30 -t 30

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python的绘图工具matplotlib使用实例
Jul 03 Python
在Python中通过threading模块定义和调用线程的方法
Jul 12 Python
从零开始学Python第八周:详解网络编程基础(socket)
Dec 14 Python
python扫描proxy并获取可用代理ip的实例
Aug 07 Python
python 简单搭建阻塞式单进程,多进程,多线程服务的实例
Nov 01 Python
python将一组数分成每3个一组的实例
Nov 14 Python
python+opencv实现阈值分割
Dec 26 Python
Python opencv实现人眼/人脸识别以及实时打码处理
Apr 29 Python
python之mock模块基本使用方法详解
Jun 27 Python
Python如何实现强制数据类型转换
Nov 22 Python
利用python读取YUV文件 转RGB 8bit/10bit通用
Dec 09 Python
python 将html转换为pdf的几种方法
Dec 29 Python
python获取Linux发行版名称
Aug 30 #Python
python实现ip地址查询经纬度定位详解
Aug 30 #Python
Django 对IP访问频率进行限制的例子
Aug 30 #Python
关于Python3 类方法、静态方法新解
Aug 30 #Python
Python 获取指定文件夹下的目录和文件的实现
Aug 30 #Python
简单的Python调度器Schedule详解
Aug 30 #Python
详解在Python中以绝对路径或者相对路径导入文件的方法
Aug 30 #Python
You might like
php中用socket模拟http中post或者get提交数据的示例代码
2013/08/08 PHP
1亿条数据如何分表100张到Mysql数据库中(PHP)
2015/07/29 PHP
JS实现切换标签页效果实例代码
2013/11/01 Javascript
jQuery实现拖动调整表格单元格大小的代码实例
2015/01/13 Javascript
jquery中one()方法的用法实例
2015/01/16 Javascript
jQuery学习笔记之jQuery中的$
2015/01/19 Javascript
Nodejs学习笔记之测试驱动
2015/04/16 NodeJs
浅谈javascript函数式编程
2015/09/06 Javascript
JavaScript中函数表达式和函数声明及函数声明与函数表达式的不同
2015/11/15 Javascript
AngularJS基础 ng-paste 指令简单示例
2016/08/02 Javascript
js实现鼠标左右移动,图片也跟着移动效果
2017/01/25 Javascript
JavaScript手风琴页面制作
2017/05/17 Javascript
关于Vue的路由权限管理的示例代码
2018/03/06 Javascript
vue mint-ui tabbar变组件使用
2018/05/04 Javascript
node(koa2) web应用模块介绍详解
2019/03/29 Javascript
Vue+Element实现网页版个人简历系统(推荐)
2019/12/31 Javascript
vue制作抓娃娃机的示例代码
2020/04/17 Javascript
python函数参数*args**kwargs用法实例
2013/12/04 Python
Python通过select实现异步IO的方法
2015/06/04 Python
Python运行报错UnicodeDecodeError的解决方法
2016/06/07 Python
python结合selenium获取XX省交通违章数据的实现思路及代码
2016/06/26 Python
DataFrame中的object转换成float的方法
2018/04/10 Python
实用自动化运维Python脚本分享
2018/06/04 Python
Python字典创建 遍历 添加等实用基础操作技巧
2018/09/13 Python
对Python _取log的几种方式小结
2019/07/25 Python
Django在pycharm下修改默认启动端口的方法
2019/07/26 Python
python中pygame安装过程(超级详细)
2019/08/04 Python
TensorFlow通过文件名/文件夹名获取标签,并加入队列的实现
2020/02/17 Python
html5的pushstate以及监听浏览器返回事件的实现
2020/08/11 HTML / CSS
美国电视购物:QVC
2017/02/06 全球购物
享誉全球的多元化时尚精品购物平台:Farfetch发发奇(支持中文)
2017/08/08 全球购物
英国时尚配饰、珠宝和服装网站:KJ Beckett
2020/01/23 全球购物
高考自主招生自荐信
2013/10/20 职场文书
模具专业毕业生自荐书范文
2014/02/19 职场文书
社区青年志愿者活动总结
2015/05/06 职场文书
你对自己的信用报告有过了解吗?
2019/07/09 职场文书