#!/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. 10.17小作业 基于TCP开发一款远程CMD程序

    基于TCP开发一款远程CMD程序 客户端连接服务器后,可以向服务器发送命令 服务器收到命令后执行,无论执行是否成功,无论执行几遍,都将执行结果返回给客户端 注意: 执行系统指令使用subprocess ...

  2. js 引号 转义字符 json字符串

    element_obj.NewTitle.value = json_obj.NewTitle.replace(/\"/g, "\""); model.NewTi ...

  3. SEO搜索引擎优化是什么?

    ㈠什么是SEO? 搜索引擎优化,又称为SEO,即Search Engine Optimization,它是一种通过分析搜索引擎的排名规律,了解各种搜索引擎怎样进行搜索.怎样抓取互联网页面.怎样确定特定 ...

  4. yum 源的搭建

    repos和epel的关系 https://blog.csdn.net/fantaxy025025/article/details/84918199 配置阿里云的yum源 https://www.cn ...

  5. python中oepen及fileobject初步整理之划水篇

    open选项 参考官方文档,很多东西也没有看懂,将自己理解的部分先整理到这里,以后还是要参阅官方文档的. open (file, mode='r', buffering=-1, encoding=No ...

  6. 暑假集训 #2 div1 I - Lada Priora 精度处理

    I - Lada Priora Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  7. Java回调

    最近在看Spring的JdbcTemplate,有碰到很多的回调场景,在这里做一个笔记. 示例: 公司的经理出差时打电话给你让你帮他处理件事情,但不能一直通着电话,于是他让你办好事情后打电话告诉他一声 ...

  8. Jmeter -- 入门,基础操作

    1. 添加线程组 设置线程组参数(线程数.准备时长.循环次数等): a)线程数:虚拟用户数.一个虚拟用户占用一个进程或线程.设置多少虚拟用户数在这里也就是设置多少个线程数. b)Ramp-Up Per ...

  9. IDEA的Python工程在inspections时提示:The JDK is not configured properly for this project. Inspection cannot proceed.

    原因:IDEA将工程误认为了Java工程. 解决方法:如果确认这是个Python工程,修改*.iml文件,将下面的这一行删除 <orderEntry type="inheritedJd ...

  10. 一、mybatis的插件介绍

    摘自:https://www.cnblogs.com/qm-article/p/11785350.html mybatis的插件机制   一.mybatis的插件介绍 关于mybatis的插件,我想大 ...