欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-05 22:47:19
这篇文章主要介绍了python采集百度搜索结果带有特定url的链接代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...

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

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