目标地址: 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】爬取新浪的栏目分类的更多相关文章

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

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

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

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

  3. python3 爬取boss直聘职业分类数据(未完成)

    import reimport urllib.request # 爬取boss直聘职业分类数据def subRule(fileName): result = re.findall(r'<p cl ...

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

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

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

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

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

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

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

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

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

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

  9. xpath爬取新浪天气

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

随机推荐

  1. (诊断)git review时出现fatal: ICLA contributor agreement requires current contact information.错误

    使用git review时出现错误: fatal: ICLA contributor agreement requires current contact information. Please re ...

  2. MYSQL 分组合并函数

    From: http://blog.csdn.net/wwwyuanliang10000/article/details/27536175 MySQL中group_concat函数完整的语法如下:gr ...

  3. mysql replace into用法详细说明

    From: http://www.cnblogs.com/martin1009/archive/2012/10/08/2714858.html REPLACE的运行与INSERT很相似.只有一点例外, ...

  4. Kong安装简介

    评价:其实是一个整合型的方案,从它的安装页面看:http://getkong.org/download/#other该方案基于OpenResty,和lua 提供的功能是统一的Oauth认证.rest封 ...

  5. u3d fpsCounter

    因为u3d自己的stats下面的fpscounter不是实际意义上的fps,所以看到demo的fpsCounter,把它改写为c#的 using UnityEngine;using System.Co ...

  6. python实现原地刷新方式输出-可用于百分比进度显示输出

    方式1: import sys sys.stdout.write('\r' + '你的输出详情') sys.stdout.flush() 方式2: print('\r' + '你的输出详情', end ...

  7. linux环境下获取真实的ip地址

    package inas.test; import java.net.Inet6Address; import java.net.InetAddress; import java.net.Networ ...

  8. SQLSERVER 2008 技术内幕 T-SQL查询 笔记1: SQL 执行顺序

    与大多数语言一样,SQL语言也有一个执行顺序,只是在大多数编程语言中,代码是按照编写顺序来处理的,而在SQL中则不是,下图为SQL 执行顺序. () ) [ ALL | DISTINCT ] () [ ...

  9. Objective-C语法之NSPredicate的使用

    正则表达式判断手机号码和电话号码的方法: #import <Foundation/Foundation.h> /** 正则判断手机号码地址格式 */ BOOL isMobileNumber ...

  10. 手机端网页使用html5地理定位获取位置失败的解决办法

    网上有很多关于html5 geolocation 获取地理定位的方法,我试了下,只有在IE edge浏览器可以成功获取到,在chrome,firefox,手机端的safari,QQ浏览器,微信浏览器, ...