Use python + xpath to get the download link

Source: Internet
Author: User

Use python + xpath to get the download link
Use python + xpath to obtain external <table class = "list">... </table> and obtain the content in <tr class = "odd"> and <tr class = "even"> respectively, when using xpath, you can write xpath ('// table [@ class = "list"]/tr [@ class = "even" or "odd"]/td/span/[ 1]/@ href ') import reimport requestsimport urllib2from 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/100'} 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 () handle T IOError: print "file not existing! "Def readfile (): try: fd = open('x.txt ', 'R') try: all_the_text = fd. read () finally: fd. close () handle T 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 after testing, you can get the corresponding download link normally. As a beginner, there are many improper aspects of the Code. Please refer to the code after reading it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.