使用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. c# float显示时保存一位小数

    UI显示时候,需求保留一位小数 //保留一位小数 参考: http://blog.sina.com.cn/s/blog_620531730100kfz6.html float ff = 1.01f; ...

  2. android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()

      最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示. main.xml <?xml version="1.0" encoding=" ...

  3. Linux计划任务.md

    crond crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程 ...

  4. 对比Haproxy和Nginx负载均衡效果

    为了对比Hproxy和Nginx负载均衡的效果,分别在测试机上(以下实验都是在单机上测试的,即负载机器和后端机器都在一台机器上)做了这两个负载均衡环境,并各自抓包分析.下面说下这两种负载均衡环境下抓包 ...

  5. 理解SVG的viewport,viewBox,preserveAspectRatio

    万丈高楼平地起,基础很重要. viewport 表示SVG可见区域的大小,或者可以想象成舞台大小,画布大小. <svg width="500" height="30 ...

  6. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. JS组件系列——BootstrapTable 行内编辑解决方案:x-editable

    前言:之前介绍bootstrapTable组件的时候有提到它的行内编辑功能,只不过为了展示功能,将此一笔带过了,罪过罪过!最近项目里面还是打算将行内编辑用起来,于是再次研究了下x-editable组件 ...

  8. VUE---Missing space before function parentheses

    解决方法:

  9. ceph hadoop spark 大数据处理

    http://docs.ceph.com/docs/giant/cephfs/hadoop/ https://indico.cern.ch/event/524549/contributions/218 ...

  10. mybatis判断传入list大小

    <if test="tenantIds.size() > 0"> AND A.PROC_TARGET_ID IN <foreach collection=& ...