#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2017-08-29 18:38:23
# @Author : EnderZhou (zptxwd@gmail.com)
# @Link : http://www.cnblogs.com/enderzhou/
# @Version : $Id$ import requests
import sys
from Queue import Queue
import threading
from bs4 import BeautifulSoup as bs
import re # 默认爬取百度76页搜索结果url,调用格式 Python.exe 本文件名称.py 搜索关键字,如关键字含特殊符号使用引号包含起来。
# 爬取结果有txt文档输出。目前尚未能过来百度推广链接,后续有可能会完善。另外后续将会添加同一网站相同路径不通参数url的过滤。
# https://www.baidu.com/s?wd=ichunqiu&pn=10
# wd参数为搜索内容关键字 pn参数控制页码 第二页为10 每页新增10 最大页数参数为750即76页。 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36',} class BaiduSpider(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
self._queue = queue def run(self):
while not self._queue.empty():
url = self._queue.get()
try:
self.spider(url)
except Exception as e:
# print e
pass def spider(self,url):
r = requests.get(url=url,headers=headers)
soup = bs(r.content,'html.parser')
urllist = soup.find_all(name='a',attrs={'data-click':re.compile(('.')),'class':None,'data-is-main-url':None})
for i in urllist:
l = requests.get(url=i['href'],headers=headers)
if l.status_code == 200:
ll = l.url.split('/')
lll = ll[0]+'//'+ll[2]+'\n'
#可根据需求修改是否显示主域名
sys.stdout.write(lll+l.url+'\n')
f1 = open('out_para.txt','a+')
f1.write(l.url+'\n')
f1.close()
with open('out_index.txt') as f:
if lll not in f.read():
f2 = open('out_index.txt','a+')
f2.write(lll)
f2.close() def main(keyword):
queue = Queue()
for i in range(0,760,10):
l = 'https://www.baidu.com/s?wd='+keyword+'&pn='+str(i)
# print l
queue.put(l)
threads = []
thread_count = 5
for i in range(thread_count):
threads.append(BaiduSpider(queue))
for t in threads:
t.start()
for t in threads:
t.join() if __name__ == '__main__':
if len(sys.argv) != 2:
print 'Enter:python %s keyword' % sys.argv[0]
sys.exit(-1)
else:
f1 = open('out_para.txt','w')
f1.close()
f2 = open('out_index.txt','w')
f2.close()
main(sys.argv[1])

python 爬取百度url的更多相关文章

  1. Python——爬取百度百科关键词1000个相关网页

    Python简单爬虫——爬取百度百科关键词1000个相关网页——标题和简介 网站爬虫由浅入深:慢慢来 分析: 链接的URL分析: 数据格式: 爬虫基本架构模型: 本爬虫架构: 源代码: # codin ...

  2. python爬取百度贴吧帖子

    最近偶尔学下爬虫,放上第二个demo吧 #-*- coding: utf-8 -*- import urllib import urllib2 import re #处理页面标签类 class Too ...

  3. 爬虫实战(一) 用Python爬取百度百科

    最近博主遇到这样一个需求:当用户输入一个词语时,返回这个词语的解释 我的第一个想法是做一个数据库,把常用的词语和词语的解释放到数据库里面,当用户查询时直接读取数据库结果 但是自己又没有心思做这样一个数 ...

  4. 假期学习【十一】Python爬取百度词条写入csv格式 python 2020.2.10

    今天主要完成了根据爬取的txt文档,从百度分类从信息科学类爬取百度词条信息,并写入CSV格式文件. txt格式文件如图: 为自己爬取内容分词后的结果. 代码如下: import requests fr ...

  5. python爬取百度搜索结果ur汇总

    写了两篇之后,我觉得关于爬虫,重点还是分析过程 分析些什么呢: 1)首先明确自己要爬取的目标 比如这次我们需要爬取的是使用百度搜索之后所有出来的url结果 2)分析手动进行的获取目标的过程,以便以程序 ...

  6. 使用python爬取百度贴吧内的图片

    1. 首先通过urllib获取网页的源码 # 定义一个getHtml()函数 def getHtml(url): try: page = urllib.urlopen(url) # urllib.ur ...

  7. python 爬取百度云资源

    pan1 1 import urllib.request 2 import re 3 import random 4 5 def get_source(key): 6 7 print('请稍等,爬取中 ...

  8. python爬取百度翻译返回:{'error': 997, 'from': 'zh', 'to': 'en', 'query 问题

    解决办法: 修改url为手机版的地址:http://fanyi.baidu.com/basetrans User-Agent也用手机版的 测试代码: # -*- coding: utf-8 -*- & ...

  9. Python爬取百度贴吧数据

    本渣除了工作外,在生活上还是有些爱好,有些东西,一旦染上,就无法自拔,无法上岸,从此走上一条不归路.花鸟鱼虫便是我坚持了数十年的爱好. 本渣还是需要上班,才能支持我的业余爱好.上班时间还是尽量访问外网 ...

随机推荐

  1. Centos 7误删Yum,如何补救???

    1. 查看centos 的版本号 cat /etc/redhat-release 2. 进入阿里云源站地址:http://mirrors.aliyun.com/  找到对应的版本号 3.下载相应的yu ...

  2. electron 设置-webkit-app-region: drag 后, 双击放大窗口变形

    双击放大后窗口只是在最左上角,并没有放大, 或者放大了页面变形,如图 原因: 是设置了窗口 transparent: true,和背景色导致的, 不要设置就可以,默认为false mainWindow ...

  3. centos7安装dockers

    安装Docker我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上, RHEL/Centos 的内核 ...

  4. 51 Nod 1134 最长递增子序列(经典问题回顾)

    1134 最长递增子序列  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元 ...

  5. 51nod 1402 最大值(贪心)

    原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1402 思路:借鉴了这篇博文http://blog.csdn.n ...

  6. 【杂题】[CodeForces 1172D] Nauuo and Portals【构造】

    Description 有一个n*n的网格,你需要在上面设置一些传送门,传送门由两个配对的格子组成,从一个进入会立刻从另一个同一方向出来. 现在有n个人从第1列出发向右走,位于(i,1)的人要走到(r ...

  7. 成功解决internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'minimatch'

    成功解决internal/modules/cjs/loader.js:596 throw err; ^ Error: Cannot find module 'minimatch' 解决办法 npm i ...

  8. phpfor函数和foreach函数

    PHP for 循环 PHP While 循环 PHP 函数 PHP for 循环执行代码块指定的次数. PHP for 循环 如果您已经提前确定脚本运行的次数,可以使用 for 循环. 语法 for ...

  9. spring-ApplicationContext的事件传播(转)

    ApplicationContext中的事件处理是通过ApplicationEvent类和ApplicationListener接口来提供的,通过ApplicationContext的publishE ...

  10. LeetCode 22. 括号生成(Generate Parentheses)

    题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "(() ...