Python爬虫实战源码合集(持续更新)

百度搜索风云榜:http://top.baidu.com/

源码:

import os
import json
from datetime import datetime
from datetime import timezone
from datetime import timedelta
from collections import OrderedDictimport requests
from bs4 import BeautifulSoupdef get_utc8now():utcnow = datetime.now(timezone.utc)utc8now = utcnow.astimezone(timezone(timedelta(hours=8)))return utc8nowdef save_as_json(filename, records):dict_obj = {}if os.path.exists(filename):with open(filename, 'r', encoding='utf-8') as f:dict_obj = json.load(f, object_pairs_hook=OrderedDict)time_str = str(get_utc8now())for keyword, search_index in records:time_count_dict = {'time': time_str, 'count': search_index}dict_obj.setdefault(keyword, []).append(time_count_dict)with open(filename, 'w', encoding='utf-8') as f:json.dump(dict_obj, f, indent=4, separators=(',',': '),ensure_ascii=False, sort_keys=False)def crawl_baidu_top(buzz_no=1):response = requests.get('http://top.baidu.com/buzz?b={}'.format(buzz_no))response.encoding = 'gb18030'soup = BeautifulSoup(response.text, 'html.parser')table_tag = soup.find('table', {'class': 'list-table'})item_tags = table_tag.find_all('tr')keywords, search_indices = [], []for item in item_tags:keyword_tag = item.find('td', {'class': 'keyword'})last_tag = item.find('td', {'class': 'last'})if (keyword_tag is not None) and (last_tag is not None):keyword_title_tag = keyword_tag.find('a', {'class': 'list-title'})keywords.append(keyword_title_tag.text.strip())search_indices.append(last_tag.text.strip())return list(zip(keywords, search_indices))if __name__ == '__main__':now = get_utc8now()year_str = now.strftime('%Y')date_str = now.strftime('%Y%m%d')os.makedirs(year_str, exist_ok=True)filename = os.path.join(year_str, '{} 实时热点.json'.format(date_str))records = crawl_baidu_top()save_as_json(filename, records)

运行:


再次运行:

Python爬取百度搜索风云榜实时热点.相关推荐

  1. Python 爬取百度 搜索风云榜 新闻并 自动推送 到邮箱

    本文将使用Python爬取百度新闻搜索指数排名前50的新闻,并通过服务器运行,每天定时发送到指定邮箱. 先上代码: # -*- coding:utf-8 -*- import requests,os, ...

  2. python爬取百度搜索_使用Python + requests爬取百度搜索页面

    想学一下怎样用python爬取百度搜索页面,因为是第一次接触爬虫,遇到一些问题,把解决过程与大家分享一下 1.使用requests爬取网页 首先爬取百度主页www.baidu.com import r ...

  3. Python爬取百度搜索的标题和真实URL的代码和详细解析

    网页爬取主要的是对网页内容进行分析,这是进行数据爬取的先决条件,因此博客主要对爬取思路进行下解析,自学的小伙伴们可以一起来学习,有什么不足也可以指出,都是在自学Ing,回归正题今天我们要来爬取百度搜索 ...

  4. python爬取百度搜索_Python-Scrapy抓取百度数据并分析

    抓取智联招聘和百度搜索的数据并进行分析,使用visual studio编写代码mongodb和SQLServer存储数据.使用scrapy框架结合 selenium爬取百度搜索数据,并进行简要的数据的 ...

  5. python 爬取百度搜索结果url

    简单的爬取百度搜索结果url 先用了requests库来访问百度,再通过xpath来提取搜索后的结果 import requests from lxml import etreefor i in ra ...

  6. python爬取百度搜索答案题目和摘要

    url就自行构造吧 # coding:utf-8 import urllib2 import re from bs4 import BeautifulSoup url = 'http://www.ba ...

  7. python爬虫代码实例-Python爬虫爬取百度搜索内容代码实例

    这篇文章主要介绍了Python爬虫爬取百度搜索内容代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 搜索引擎用的很频繁,现在利用Python爬 ...

  8. 【Spark分布式内存计算框架——Spark Streaming】10. 应用案例:百度搜索风云榜(中)实时数据ETL存储

    5.3 实时数据ETL存储 实时从Kafka Topic消费数据,提取ip地址字段,调用[ip2Region]库解析为省份和城市,存储到HDFS文件中,设置批处理时间间隔BatchInterval为1 ...

  9. python 将百度搜索风云榜的关键内容提取并写入txt文件和读取验证

    # -*- coding:UTF-8 -*- from bs4 import BeautifulSoup import requests,sys import codecs##今日娱乐名人排行榜--百 ...

最新文章

  1. 关于C#中编译器保证变量必须初始化规则猜想
  2. Java中@Override的作用
  3. 6.1 无监督学习-机器学习笔记-斯坦福吴恩达教授
  4. jquery 插件和后台模板搜集
  5. Android继承AppCompatActivity实现全屏设置
  6. HDU 2676 Matrix
  7. Unix/Linux环境C编程入门教程(20) 搭建基于Mac的 Xcode 与 QT 开发环境
  8. Web API应用架构在Winform混合框架中的应用(5)--系统级别字典和公司级别字典并存的处理方式...
  9. StringUtil类的常用方法
  10. VC编程实现运行Excel宏 Run
  11. Win11动态磁贴没了?Win11中恢复动态磁贴的方法
  12. 面试问到UDP,这样回答offer拿到手软
  13. ce变速注入dll失败_调用CE变速DLL注入的软件含HOOK模块
  14. python中idle_python中的idle是什么意思
  15. (十四)商品详情页实现
  16. word文档字不靠边_word怎么调整单元格文字边距表格文字紧靠边框怎么办
  17. Machine Learning读书会,面试算法讲座,创业活动,算法班(15年6月)
  18. 吴恩达深度学习课程笔记(初步认识神经网络)
  19. 【OpenCV 例程300篇】202. 查表快速替换(cv.LUT)
  20. android 乐固渠道打包,安卓腾讯乐固(legutools)多渠道打包(友盟)

热门文章

  1. postgre ~模糊查询慢解决方式
  2. SQL中关于where后面不能放聚合函数(如sum等)的解决办法
  3. Xcode C++ and Objective-C refactoring
  4. 初识webservice 服务
  5. oracle-3-子查询和常用函数
  6. 误删除了Oracle的dbf文件后的解决方法
  7. 搜索 + 剪枝 --- POJ 1101 : Sticks
  8. cacti+nagios整合(未成)
  9. string 转换int
  10. cobertura试用