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设计】。

相关文章

详解pandas使用drop_duplicates去除DataFrame重复项参数

Pandas之drop_duplicates:去除重复项 方法 DataFrame.drop_duplicates(subset=None, keep='first', inpl...

python输入错误后删除的方法

python输入错误怎么删除? python常用的输入函数raw_input()在输入的过程中如果输错了,不能像在命令行下那样backspace取消已输入的字符,还得重新再输入。 怎么才...

python装饰器简介---这一篇也许就够了(推荐)

Python装饰器(decorator)是在程序开发中经常使用到的功能,合理使用装饰器,能让我们的程序如虎添翼。 装饰器引入 初期及问题诞生 假如现在在一个公司,有A B C三个业务部门...

Tensorflow中使用tfrecord方式读取数据的方法

Tensorflow中使用tfrecord方式读取数据的方法

前言 本博客默认读者对神经网络与Tensorflow有一定了解,对其中的一些术语不再做具体解释。并且本博客主要以图片数据为例进行介绍,如有错误,敬请斧正。 使用Tensorflow训练神...

Python中用max()方法求最大值的介绍

 max() 方法返回其参数最大值:最接近正无穷大的值。 语法 以下是max()方法的语法: max( x, y, z, .... ) 参数  &nb...