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

yipeiwu_com6年前Python基础

这篇文章主要介绍了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设计】。

相关文章

Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例

本文实例讲述了Flask框架重定向,错误显示,Responses响应及Sessions会话操作。分享给大家供大家参考,具体如下: 重定向和错误显示 将用户重定向到另一个端点,使用redi...

python递归查询菜单并转换成json实例

python递归查询菜单并转换成json实例

最近需要用python写一个菜单,折腾了两三天才搞定,现在记录在此,需要的朋友可以借鉴一下。 备注:文章引用非可执行完整代码,仅仅摘录了关键部分的代码 环境 数据库:mysql...

Python 生成器,迭代,yield关键字,send()传参给yield语句操作示例

Python 生成器,迭代,yield关键字,send()传参给yield语句操作示例

本文实例讲述了Python 生成器,迭代,yield关键字,send()传参给yield语句操作。分享给大家供大家参考,具体如下: demo.py(生成器,yield关键字): #...

Python中输出ASCII大文字、艺术字、字符字小技巧

Python中输出ASCII大文字、艺术字、字符字小技巧

复制代码 代码如下: display text in large ASCII art fonts 显示大ASCII艺术字体 这种东西在源码声明或者软件初始化控制台打印时候很有用。...

Ubuntu16.04安装python3.6.5步骤详解

下载python3.6.5安装包 1.   上传安装包。打开终端,利用命令cd 进入文件所在文件夹里 python@ubuntu:~/workspace$pwd...