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. 1_Layui

    一. 引言 官网: https://www.layui.com/ 在官网首页, 可以很方便的下载Layui Layui是一款经典模块化前端UI框架, 我们只需要定义简单的HTML,CSS,JS即可实现 ...

  2. WinDbg Preview安装以及符号表配置

    1.安装WinDbgPreview 在Microsoft Store直接搜索windbg就可以下载. 2.配置符号服务器 2.1 符号 符号是方便调试程序的文件,通常是pdb文件.一个模块(可执行程序 ...

  3. C#中Math.Round()实现中国式四舍五入问题

    C#中的Math.Round()并不是使用的"四舍五入"法.实际上是四舍六入五取偶. 对于这个问题我们常见的解决方式是使用MidpointRounding.AwayFromZero ...

  4. Spring Boot 配置 jar 包外面的 Properties 配置文件

    一.概述 Properties 文件是我们可以用来存储项目特定信息的常用方法.理想情况下,我们应该将其保留在 jar 包之外,以便能够根据需要对配置进行更改. 在这个教程中,我们将研究在 Spring ...

  5. Hive之权限控制

    Hive之权限 一.库级的权限 -- 授予所有权限给某个用户 grant all on msta to user {userName}; -- 授权admin privilege权限 grant AD ...

  6. 华为设备配置和使用FTP服务命令

    配置SFTP Server与Client server:aaa 进入aaa视图 local-user huawei2 password cipher huawei2 设置用户名和密码 local-us ...

  7. 2022-08-12-esp32把玩记-②_用Micropython点ssd1306_oled屏幕

    layout: post cid: 8 title: esp32把玩记-② 用Micropython点ssd1306 oled屏幕 slug: 8 date: 2022/08/12 15:12:39 ...

  8. python-D2-计算机与编程语言

    计算机五大核心 控制器 计算机的指挥系统,可以控制计算机硬件的整体运行 运算器 实现算术运算和逻辑运算 控制器和运算器结合起来就是cpu,也称为中央处理器,是整个电脑的核心. 存储器 分为两类,非永久 ...

  9. SQL中的转义字符和通配符

    一.通配符 如果想查找"_cs"结尾的的账户   select * from [user] where loginname like '%_cs'是不行的,  _ 被认为是任意的字 ...

  10. Razor中RenderBoby的使用

    1. RenderBody 在Razor引擎中没有了"母版页",取而代之的是叫做"布局"的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面 ...