1. import requests
  2. import cchardet
  3. import traceback
  4. from lxml import etree
  5.  
  6. def downloader(url,timeout = 10,headers = None,debug = False, binary = False):
  7. _headers = {
  8. 'User-Agent': ('Mozilla/5.0 (compatible; MSIE 9.0; '
  9. 'Windows NT 6.1; Win64; x64; Trident/5.0)')
  10. }
  11. redirected_url = url
  12. if headers:
  13. headers = _headers
  14. try:
  15. res = requests.get(url,headers,timeout = timeout)
  16. if binary:
  17. html = res.content
  18. else:
  19. encoding = cchardet.detect(res.content)["encoding"]
  20. html = res.content.decode(encoding)
  21. status = res.status_code
  22. redirected_url = res.url
  23. except:
  24. if debug:
  25. traceback.print_exc()
  26. msg = "failed download:{}".format(url)
  27. print(msg)
  28. if binary:
  29. html =b""
  30. else:
  31. html = ""
  32. status = 0
  33. return status,html,redirected_url
  34.  
  35. def parser(html):
  36. d = 0
  37. tree = etree.HTML(html)
  38. divs_list = tree.xpath(".//div[@class = 'main']/div[contains(@class,'clearfix')]")
  39. for div in divs_list:
  40. a_list = div.xpath(".//ul[contains(@class,'list-a')]//a")
  41. for i in a_list:
  42. try:
  43. href = i.xpath("./@href")[0].strip().replace("\\n",'').replace('\\t','')
  44. title = i.xpath("./text()")[0].strip().replace("\\n",'').replace('\\t','')
  45. d += 1
  46. print(d,(href,title))
  47. except (IndexError) as e:
  48. pass
  49.  
  50. if __name__ == '__main__':
  51. url = r"https://www.sina.com.cn/"
  52. status,html,redirected_url = downloader(url)
  53. paser = parser(html)
  54. #print(status,html,redirected_url)

大规模爬取(新浪为例子)网页之downloader、parser的封装(涉及编码等细节)的更多相关文章

  1. selenium+BeautifulSoup+phantomjs爬取新浪新闻

    一 下载phantomjs,把phantomjs.exe的文件路径加到环境变量中,也可以phantomjs.exe拷贝到一个已存在的环境变量路径中,比如我用的anaconda,我把phantomjs. ...

  2. Python3:爬取新浪、网易、今日头条、UC四大网站新闻标题及内容

    Python3:爬取新浪.网易.今日头条.UC四大网站新闻标题及内容 以爬取相应网站的社会新闻内容为例: 一.新浪: 新浪网的新闻比较好爬取,我是用BeautifulSoup直接解析的,它并没有使用J ...

  3. Python 爬虫实例(7)—— 爬取 新浪军事新闻

    我们打开新浪新闻,看到页面如下,首先去爬取一级 url,图片中蓝色圆圈部分 第二zh张图片,显示需要分页, 源代码: # coding:utf-8 import json import redis i ...

  4. python3爬虫-爬取新浪新闻首页所有新闻标题

    准备工作:安装requests和BeautifulSoup4.打开cmd,输入如下命令 pip install requests pip install BeautifulSoup4 打开我们要爬取的 ...

  5. python2.7 爬虫初体验爬取新浪国内新闻_20161130

    python2.7 爬虫初学习 模块:BeautifulSoup requests 1.获取新浪国内新闻标题 2.获取新闻url 3.还没想好,想法是把第2步的url 获取到下载网页源代码 再去分析源 ...

  6. python3使用requests爬取新浪热门微博

    微博登录的实现代码来源:https://gist.github.com/mrluanma/3621775 相关环境 使用的python3.4,发现配置好环境后可以直接使用pip easy_instal ...

  7. python爬取新浪股票数据—绘图【原创分享】

    目标:不做蜡烛图,只用折线图绘图,绘出四条线之间的关系. 注:未使用接口,仅爬虫学习,不做任何违法操作. """ 新浪财经,爬取历史股票数据 ""&q ...

  8. xpath爬取新浪天气

    参考资料: http://cuiqingcai.com/1052.html http://cuiqingcai.com/2621.html http://www.cnblogs.com/jixin/p ...

  9. 【python3】爬取新浪的栏目分类

    目标地址: http://www.sina.com.cn/ 查看源代码,分析: 1 整个分类 在 div main-nav 里边包含 2 分组情况:1,4一组 . 2,3一组 . 5 一组 .6一组 ...

  10. python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例

    python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...

随机推荐

  1. 4.第三篇 PKI基础概念、cfssl工具介绍及kubernetes中证书

    文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247483787&idx=1&sn=08dd3404 ...

  2. 第三章:模版层 - 2:Django内置模板标签

    Django内置标签总览 可以查询下表来总览Django的内置标签: 标签 说明 autoescape 自动转义开关 block 块引用 comment 注释 csrf_token CSRF令牌 cy ...

  3. FastDFS与nginx配置使用的配置信息

    # 获取图片 location /group[1-9]/M0[0-9] { root /home/vdc1/fastdfs_storage/data; ngx_fastdfs_module; } # ...

  4. 原生js如果将string类型的数进行值

    原生的tring类型比较会进行隐式转换,如'100'>90 为true

  5. 【可视化大屏教程】用Python开发智慧城市数据分析大屏!

    目录 一.开发背景 二.讲解代码 2.1 大标题+背景图 2.2 各区县交通事故统计图-系列柱形图 2.3 图书馆建设率-水球图 2.4 当年城市空气质量aqi指数-面积图 2.5 近7年人均生产总值 ...

  6. 不安装运行时运行 .NET 程序 - NativeAOT

    大家好,先祝大家国庆快乐.不过大家看到这篇文章的时候估计已经过完国庆了 . 上一篇我们写了如何通过 SelfContained 模式发布程序(不安装运行时运行.NET程序)达到不需要在目标机器上安装 ...

  7. 洛谷P2866 [USACO06NOV]Bad Hair Day S (单调栈)

    看到这道题很容易想到单调栈,但我一开始想的是从后往前扫,但发现会有问题(因为这样会对后面牛的答案造成影响),所以这时我们要及时换一个思路,从前往后扫. 维护一个单调递减的栈,插入h[i]时,小等于它的 ...

  8. pgsql 的问题

    pgsql 怎么插入inet类型的数据?insert into table (remote_addr) values ( ?::INET); pgsql如何截取时间的精度 select  create ...

  9. Docker | 使用dockerfile生成镜像,清理docker空间

    用dockerfile生成镜像并挂载数据卷 编写dockerfile文件 创建dockerfile01 文件 # 基础镜像 FROM centos VOLUME ["volume01&quo ...

  10. Centos7使用sendEmail-v1.56发送邮件

    Centos7使用sendEmail-v1.56发送邮件 注意:Centos7默认使用perl5.16,而sendEmail-v.1.56要求使用perl5.10.否则会报以下错误.所以需要下载并安装 ...