import requests
import cchardet
import traceback
from lxml import etree def downloader(url,timeout = 10,headers = None,debug = False, binary = False):
_headers = {
'User-Agent': ('Mozilla/5.0 (compatible; MSIE 9.0; '
'Windows NT 6.1; Win64; x64; Trident/5.0)')
}
redirected_url = url
if headers:
headers = _headers
try:
res = requests.get(url,headers,timeout = timeout)
if binary:
html = res.content
else:
encoding = cchardet.detect(res.content)["encoding"]
html = res.content.decode(encoding)
status = res.status_code
redirected_url = res.url
except:
if debug:
traceback.print_exc()
msg = "failed download:{}".format(url)
print(msg)
if binary:
html =b""
else:
html = ""
status = 0
return status,html,redirected_url def parser(html):
d = 0
tree = etree.HTML(html)
divs_list = tree.xpath(".//div[@class = 'main']/div[contains(@class,'clearfix')]")
for div in divs_list:
a_list = div.xpath(".//ul[contains(@class,'list-a')]//a")
for i in a_list:
try:
href = i.xpath("./@href")[0].strip().replace("\\n",'').replace('\\t','')
title = i.xpath("./text()")[0].strip().replace("\\n",'').replace('\\t','')
d += 1
print(d,(href,title))
except (IndexError) as e:
pass if __name__ == '__main__':
url = r"https://www.sina.com.cn/"
status,html,redirected_url = downloader(url)
paser = parser(html)
#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. Elasticsearch: Join数据类型

    在Elasticsearch中,Join可以让我们创建parent/child关系.Elasticsearch不是一个RDMS.通常join数据类型尽量不要使用,除非不得已.那么Elasticsear ...

  2. MySQL用户中的%到底包不包括localhost?

    1 前言 操作MySQL的时候发现,有时只建了%的账号,可以通过localhost连接,有时候却不可以,网上搜索也找不到满意的答案,干脆手动测试一波 2 两种连接方法 这里说的两种连接方法指是执行my ...

  3. 多字段特性及配置自定义Analyzer

    PUT logs/_doc/1 {"level":"DEBUG"} GET /logs/_mapping POST _analyze { "token ...

  4. 如何0到1构建DevOps?

    从0到1构建DevOps,首先得弄清楚这个DevOps的受众群体,它的用途到底是什么,解决什么问题,比如Android Studio是为了解决Android应用的开发,3UCS xPlus是为了解决应 ...

  5. 如何去了解Spring

    对于你想了解的技术 官方总是一个合适的选择 首先,我们所指的Spring 一般指的是Spring Framework,伴随着的时代的进步,Spring全家桶也逐渐完善起来 Spring 1.Why S ...

  6. Python生成10个八位随机密码

    #生成10个八位随机密码 import random lst1=[ chr(i) for i in range(97,123) ] #生成26为字母列表 lst2=[i for i in range( ...

  7. NLP之基于词嵌入(WordVec)的嵌入矩阵生成并可视化

    词嵌入 @ 目录 词嵌入 1.理论 1.1 为什么使用词嵌入? 1.2 词嵌入的类比推理 1.3 学习词嵌入 1.4 Word2Vec & Skip-Gram(跳字模型) 1.5 分级& ...

  8. AgileBoot - 如何集成内置数据库H2和内置Redis

    本项目地址: github: https://github.com/valarchie/AgileBoot-Back-End gitee: https://gitee.com/valarchie/Ag ...

  9. 三、docker镜像管理

    一.docker镜像管理 1.1.镜像搜索-search 从docker镜像仓库模糊搜索镜像 用法: docker search 镜像关键字 [root@zutuanxue ~]# docker se ...

  10. clip-path属性深入理解与使用

    clip-path CSS 属性可以创建一个只有元素的部分区域可以显示的剪切区域.区域内的部分显示,区域外的隐藏. clip-path的属性值可以是以下几种: 1.inset: 将元素剪裁为一个矩形, ...