python 爬取百度url
#!/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的更多相关文章
- Python——爬取百度百科关键词1000个相关网页
Python简单爬虫——爬取百度百科关键词1000个相关网页——标题和简介 网站爬虫由浅入深:慢慢来 分析: 链接的URL分析: 数据格式: 爬虫基本架构模型: 本爬虫架构: 源代码: # codin ...
- python爬取百度贴吧帖子
最近偶尔学下爬虫,放上第二个demo吧 #-*- coding: utf-8 -*- import urllib import urllib2 import re #处理页面标签类 class Too ...
- 爬虫实战(一) 用Python爬取百度百科
最近博主遇到这样一个需求:当用户输入一个词语时,返回这个词语的解释 我的第一个想法是做一个数据库,把常用的词语和词语的解释放到数据库里面,当用户查询时直接读取数据库结果 但是自己又没有心思做这样一个数 ...
- 假期学习【十一】Python爬取百度词条写入csv格式 python 2020.2.10
今天主要完成了根据爬取的txt文档,从百度分类从信息科学类爬取百度词条信息,并写入CSV格式文件. txt格式文件如图: 为自己爬取内容分词后的结果. 代码如下: import requests fr ...
- python爬取百度搜索结果ur汇总
写了两篇之后,我觉得关于爬虫,重点还是分析过程 分析些什么呢: 1)首先明确自己要爬取的目标 比如这次我们需要爬取的是使用百度搜索之后所有出来的url结果 2)分析手动进行的获取目标的过程,以便以程序 ...
- 使用python爬取百度贴吧内的图片
1. 首先通过urllib获取网页的源码 # 定义一个getHtml()函数 def getHtml(url): try: page = urllib.urlopen(url) # urllib.ur ...
- python 爬取百度云资源
pan1 1 import urllib.request 2 import re 3 import random 4 5 def get_source(key): 6 7 print('请稍等,爬取中 ...
- python爬取百度翻译返回:{'error': 997, 'from': 'zh', 'to': 'en', 'query 问题
解决办法: 修改url为手机版的地址:http://fanyi.baidu.com/basetrans User-Agent也用手机版的 测试代码: # -*- coding: utf-8 -*- & ...
- Python爬取百度贴吧数据
本渣除了工作外,在生活上还是有些爱好,有些东西,一旦染上,就无法自拔,无法上岸,从此走上一条不归路.花鸟鱼虫便是我坚持了数十年的爱好. 本渣还是需要上班,才能支持我的业余爱好.上班时间还是尽量访问外网 ...
随机推荐
- C++--- Lambda匿名函数表达式
视频教程:https://www.bilibili.com/video/av66419552/ 格式: [capture](parameters)mutable->returm-type{}; ...
- Redis常用数据类型底层数据结构分析
Redis是一种键值(key-Value)数据库,相对于关系型数据库,它也被叫作非关系型数据库 Redis中,键的数据类型是字符串,但是为了非富数据存储方式,方便开发者使用,值的数据类型有很多 字符串 ...
- idea 导入(非maven)web项目并发布到tomcat服务器
IDEA 2017.1版本 web项目导入并发布到Tomcat服务器 1.点击编辑项目结构 2.点击project 将项目编译输出目录改成{项目目录}/OUT,并设置项目环境,编译版本 3.点击mod ...
- 多版本python的pip 升级后, pip2 pip3 与python版本失配
mint19.2 本来pip 和 pip2 对应 python2.7 pip3对应python3.6 用源码安装了python3.7之后. 这样 版本也没问题. 但是, 用pip3.7 安装 ...
- [LOJ3046][ZJOI2019]语言:树链的并+线段树合并
分析 问题显然可以转化为对于每个节点询问所有这个节点的所有链的链并的大小. 考场上我直接通过树剖打标记+树剖线段树维护以\(O(n \log^3 n)\)的时间复杂度暴力实现了这个过程.(使用LCT或 ...
- Apicloud_(模板)登陆注册功能模板
项目已托管到Github上 传送门 不需要使用任何图片资源,需要用到SHA1.js库文件, Apicloud_(接口验证)用户注册头部信息X-APICloud-AppKey生成 传送门 项目全代码放到 ...
- centOS7安装docker遇到 [Errno 14] curl#35 - "TCP connection reset by peer问题解决
---------------------------------------------------------------------------------------------------- ...
- [BZOJ2286][Sdoi2011]消耗战(虚树上DP)
2286: [Sdoi2011]消耗战 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6457 Solved: 2533[Submit][Statu ...
- JSON格式标准
JSON格式 json的基本类型有objects(dicts), arrays(lists), strings, numbers, booleans, and nulls(json中关键字).在一个o ...
- Div内容居中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...