xpath爬取Python100例


#!/usr/bin/env python
# _*_coding:utf-8 _*_
# @Time     :2017/8/26 15:55
# @Author   :luoyu_bie
# @File     :python_100_example.py
# @Software :PyCharm Community Edition
#导入模块
import requests
import urlparse
#转换文件编码
import codecs
from lxml import etree

#步骤
#1、获取li标签
#2、遍历li里面的题目
#3、获取下一页的链接
#4、分页处理,实现回调
#(返回爬取到的结果及下一页的链接)
agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'
headers = {"User-Agent":agent}

def get_tag_list(url):
    response = requests.get(url,headers)
    html = response.content
    ht = etree.HTML(html)
    items = ht.xpath('/html/body/div[3]/div/div[2]/div/div[3]/div/ul[1]/li/a/@href')
    for url in items:
        #返回一个可迭代对象
        yield 'http://www.runoob.com/'+url

def get_text(url_list):
    for item in url_list:
        html = requests.get(item).content
        ht = etree.HTML(html)
        title = ht.xpath('//*[@id="content"]/h1/text()')[0] #获取h1标签的文本内容
        #position()得到相应的标签的序号
        content_list1 = ht.xpath('//*[@id="content"]/p[position()>1 and position()<4]/text()')
        #join把列表链接为字符串
        content = '\n'.join(content_list1)
        yield (title,content)


url = "http://www.runoob.com/python/python-100-examples.html"
#获取所有题目页面的链接
url_list = get_tag_list(url)
#获取所有详细的页面数据
content_list = get_text(url_list)
with codecs.open('1.txt','w+','utf-8') as f:
    for title,content in content_list:
        f.write(title+'\n')
        f.write(content+'\n')
        f.write('*'*20+'\n')



  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值