【python3】爬取新浪的栏目分类
目标地址: http://www.sina.com.cn/

查看源代码,分析:
1 整个分类 在 div main-nav 里边包含
2 分组情况:1,4一组 、 2,3一组 、 5 一组 、6一组

实现源码:
# coding=utf-8
import urllib.request
import ssl
from lxml import etree # 获取html内容
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
html = html.decode('utf-8')
return html # 获取内容
def get_title(arr, html, pathrole, sumtimes):
selector = etree.HTML(html)
content = selector.xpath(pathrole)
i = 0
while i <= sumtimes:
result = content[i].xpath('string(.)').strip()
arr.append(result)
i += 1
return arr # 创建ssl证书
ssl._create_default_https_context = ssl._create_unverified_context
url = "http://www.sina.com.cn/"
html = getHtml(url)
# 第一次获取
arr = []
pathrole1 = '//div[@class="main-nav"]/div[@class="nav-mod-1 nav-w"]/ul/li'
retult1 = get_title(arr, html, pathrole=pathrole1, sumtimes=23) # 第二次获取
if retult1:
pathrole2 = '//div[@class="main-nav"]/div[@class="nav-mod-1"]/ul/li'
retult2 = get_title(retult1, html, pathrole=pathrole2, sumtimes=23)
else:
print("error") # 第三次获取
if retult2:
pathrole3 = '//div[@class="main-nav"]/div[@class="nav-mod-1 nav-mod-s"]/ul/li'
retult3 = get_title(retult2, html, pathrole3, sumtimes=11)
else:
print("error") # 第四次获取
if retult3:
pathrole4 = '//div[@class="main-nav"]/div[@class="nav-mod-1 nav-w nav-hasmore"]/ul/li'
retult4 = get_title(retult3, html, pathrole4, sumtimes=1)
else:
print("error") # 第五次获取:更多列表
if retult4:
pathrole5 = '//div[@class="main-nav"]/div[@class="nav-mod-1 nav-w nav-hasmore"]/ul/li/ul[@class="more-list"]/li'
retult5 = get_title(retult4, html, pathrole5, sumtimes=6)
print(retult5)
else:
print("error")
以上代码,还可以继续优化,比如 xpath 的模糊匹配。可以把前四组合为一个,继续学习!
【python3】爬取新浪的栏目分类的更多相关文章
- Python3:爬取新浪、网易、今日头条、UC四大网站新闻标题及内容
Python3:爬取新浪.网易.今日头条.UC四大网站新闻标题及内容 以爬取相应网站的社会新闻内容为例: 一.新浪: 新浪网的新闻比较好爬取,我是用BeautifulSoup直接解析的,它并没有使用J ...
- selenium+BeautifulSoup+phantomjs爬取新浪新闻
一 下载phantomjs,把phantomjs.exe的文件路径加到环境变量中,也可以phantomjs.exe拷贝到一个已存在的环境变量路径中,比如我用的anaconda,我把phantomjs. ...
- python3 爬取boss直聘职业分类数据(未完成)
import reimport urllib.request # 爬取boss直聘职业分类数据def subRule(fileName): result = re.findall(r'<p cl ...
- python3爬虫-爬取新浪新闻首页所有新闻标题
准备工作:安装requests和BeautifulSoup4.打开cmd,输入如下命令 pip install requests pip install BeautifulSoup4 打开我们要爬取的 ...
- python3使用requests爬取新浪热门微博
微博登录的实现代码来源:https://gist.github.com/mrluanma/3621775 相关环境 使用的python3.4,发现配置好环境后可以直接使用pip easy_instal ...
- Python 爬虫实例(7)—— 爬取 新浪军事新闻
我们打开新浪新闻,看到页面如下,首先去爬取一级 url,图片中蓝色圆圈部分 第二zh张图片,显示需要分页, 源代码: # coding:utf-8 import json import redis i ...
- python2.7 爬虫初体验爬取新浪国内新闻_20161130
python2.7 爬虫初学习 模块:BeautifulSoup requests 1.获取新浪国内新闻标题 2.获取新闻url 3.还没想好,想法是把第2步的url 获取到下载网页源代码 再去分析源 ...
- python爬取新浪股票数据—绘图【原创分享】
目标:不做蜡烛图,只用折线图绘图,绘出四条线之间的关系. 注:未使用接口,仅爬虫学习,不做任何违法操作. """ 新浪财经,爬取历史股票数据 ""&q ...
- xpath爬取新浪天气
参考资料: http://cuiqingcai.com/1052.html http://cuiqingcai.com/2621.html http://www.cnblogs.com/jixin/p ...
随机推荐
- webstorm 重置所有设置
我的是win10的,删除如下路径的文件夹C:\Users\XXX(你自己电脑用户名)\.IntelliJIdeaxxxx(版本号) 这是最快捷的方法
- js json日期格式转换日期方法
接收json数据,日期格式为:"\/Date(1414078309687)\/" var value = "/Date(1414078309687)/"; ...
- GCT之数学公式(三角函数)
- hbase深入了解
http://blog.csdn.net/u010967382/article/details/37878701?utm_source=tuicool&utm_medium=referral ...
- 修改vs2005,vs2008,vs2010调试默认浏览器
前些日子不小心安装上了一个sogou的浏览器,感觉这个浏览器用起来也算方便,所以就么有卸载,一直就这么用着,但当我用vs来调试web程序的 时候问题出来了,默认的调试浏览器变成了搜狗的浏览器了,我在v ...
- ubuntu14.04 允许root用ssh登录
重启ssh service ssh restart 查看ssh是否已安装 ssh进程 ps -e | grep ssh ssh占用端口 lsof -i:22 ssh服务已启动,但是用root账号无法连 ...
- mysql中查看某个日期是星期几?如何知道某个日期是星期几?某个日期是周几?
需求描述: mysql中,如果要查看某个日期是星期几,可以用date_format函数实现,在此记录下. 操作过程: 1.通过date_format函数查看某个日期是星期几 mysql> sel ...
- mysql中json_replace函数的使用?通过json_replace对json对象的值进行替换
需求描述: 在看mysql中关于json的内容,通过json_replace函数可以实现对json值的替换, 在此记录下. 操作过程: 1.查看带有json数据类型的表 mysql> selec ...
- 通过phoenix在hbase上创建二级索引,Secondary Indexing
环境描述: 操作系统版本:CentOS release 6.5 (Final) 内核版本:2.6.32-431.el6.x86_64 phoenix版本:phoenix-4.10.0 hbase版本: ...
- [Bayesian] “我是bayesian我怕谁”系列 - Naive Bayes with Prior
先明确一些潜规则: 机器学习是个collection or set of models,一切实践性强的模型都会被归纳到这个领域,没有严格的定义,’有用‘可能就是唯一的共性. 机器学习大概分为三个领域: ...