使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接:

使用requests获取html后,分析html中的标签发现所需要的链接在<table class="list" >...</table> 中

然后分别获却<tr class="odd"> 和<tr class="even">中的内容 ,使用xpath时可以写成xpath('//table[@class="list"]/tr[@class="even" or "odd"]/td/span/a[1]/@href')

import re

import requests

import urllib2

from lxml import etree

url='https://pypi.python.org/pypi/lxml/2.3/'

head={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'}

def gethtml(url, *args):

    html = requests.get(url, *args).content

    return html

def writfile(cont):

    try:

        fd = open('x.txt', 'w')

        try:

            fd.write(cont)

        finally:

            fd.close()

    except IOError:

        print "file not existing!"

def readfile():

    try:

        fd = open('x.txt', 'r')

        try:

            all_the_text = fd.read()

        finally:

            fd.close()

    except IOError:

        print "File open error !"

    return all_the_text

html = gethtml(url, head)

writfile(html)

all_text = readfile()

dom = etree.HTML(all_text)

url_list = dom.xpath('//table[@class="list"]/tr[@class="even" or "odd"]/td/span/a[1]/@href')

for url in url_list:

    print url

 

使用python+xpath 获取https://pypi.python.org/pypi/lxml/2.3/的下载链接的更多相关文章

  1. 自学 Python 3 最好的 入门 书籍 推荐(附 免费 在线阅读 下载链接)

    请大家根据自己的实际情况对号入座,挑选适合自己的 Python 入门书籍: 完全没有任何编程基础:01 号书 少量编程基础,不求全,只希望能以最快的速度入门:02 号书 少量编程基础,有一定的英文阅读 ...

  2. Python深入:Distutils发布Python模块--转载

    https://blog.csdn.net/gqtcgq/article/details/49255995 Distutils可以用来在Python环境中构建和安装额外的模块.新的模块可以是纯Pyth ...

  3. Python 标准库一览(Python进阶学习)

    转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...

  4. Python深入:Distutils发布Python模块

    Distutils可以用来在Python环境中构建和安装额外的模块.新的模块可以是纯Python的,也可以是用C/C++写的扩展模块,或者可以是Python包,包中包含了由C和Python编写的模块. ...

  5. Python系列:一、Python概述与环境安装--技术流ken

    Python简介 Python是一种计算机程序设计语言.是一种动态的.面向对象的脚本语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项 ...

  6. HanLP https://pypi.python.org/pypi/sumy/

    HanLP - 汉语言处理包 http://hanlp.linrunsoft.com/doc.html https://pypi.python.org/pypi/sumy/

  7. 解决 ‘Could not fetch URL https://pypi.python.org’的问题

    [前提]: win10下python3和python2共存环境,但是环境变量只配置了python3 [问题]: 用pip安装一个包执行pip2 install xxx的时候报错Fatal error ...

  8. 日常问题--解决 ‘Could not fetch URL https://pypi.python.org’的问题

    难题描述: 解决方法: 使用命令python -m pip install Scrapy  --trusted-host=pypi.python.org --trusted-host=pypi.org ...

  9. 在CentOS下利用Python+selenium获取腾讯首页的今日话题。

    1.安装依赖包 yum install wget firefox gcc zlib zlib-devel Xvfb 2.安装setuptools 官网地址:https://pypi.python.or ...

随机推荐

  1. 用grunt搭建web前端开发环境

    1.前言 本文章旨在讲解grunt入门,以及讲解grunt最常用的几个插件的使用. 2.安装node.js Grunt和所有grunt插件都是基于nodejs来运行的,如果你的电脑上没有nodejs, ...

  2. jQuery选择器和选取方法 http://www.cnblogs.com/MaxIE/p/4078869.html

    我们已经使用了带有简单Css选择器的jQuery选取函数:$().现在是时候深入了解jQuery选择器语法,以及一些提取和扩充选中元素集的方法了. 一.jQuery选择器 在CSS3选择器标淮草案定义 ...

  3. 【转】40条常见的移动端Web页面问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊                  2.图片加载                            3.假如手机网站不用兼容IE浏览器,一般我们会使用zep ...

  4. Windows 上如何安装Sqlite

    对SQLite文明已久,却是从来没使用过,今天就来安装试用下. 一.安装 下载地址:http://www.sqlite.org/download.html 将Precompiled Binaries ...

  5. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  6. 微信随机红包(Java)

    概述 最近受一朋友提醒,问微信红包怎么实现的,当时思考了一下,觉得好像很容易,可是当真正实现的时候,发现其中有不少问题,于是小白博主查阅资料,其中资料主要来源于知乎的一篇讨论<微信红包的随机算法 ...

  7. [网站性能3]SqlServer中Profiler的使用

    原文链接:http://www.cnblogs.com/caishuhua226/p/3838060.html   http://www.cnblogs.com/lyhabc/articles/294 ...

  8. celery 框架

    转自:http://www.cnblogs.com/forward-wang/p/5970806.html 生产者消费者模式 在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据 ...

  9. Maven的pom报maven-surefire-plugin:pom:2.12.4

    新建Maven项目,报错: Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from htt ...

  10. sql 删除所有表

    sql删除所有表语句: use 数据库名(是要删除表的所在的那个数据库的名称) GO declare @sql varchar(8000) while (select count(*) from sy ...