0

Environment:

Window 7

Python 2.7.6

Chrome

I am using python 2.7.6 and trying to download the image from the following link. Only 1 image in the link.

Image

But I failed get the address of the image using lxml. Here is my code.

from urllib2 import urlopen
from lxml import etree
import lxml.html

url = 'http://charts.aastocks.com/servlet/Charts?fontsize=12&15MinDelay=T&lang=0&titlestyle=1&vol=1&Indicator=1&indpara1=10&indpara2=20&indpara3=50&indpara4=100&indpara5=150&subChart1=2&ref1para1=14&ref1para2=0&ref1para3=0&subChart2=3&ref2para1=12&ref2para2=26&ref2para3=9&subChart3=12&ref3para1=0&ref3para2=0&ref3para3=0&scheme=3&com=100&chartwidth=1254&chartheight=1058&stockid=110000.HK&period=6&type=1&logoStyle=1&'

tree = etree.HTML(urlopen(url).read())
for img in tree.xpath('/html/body/img'):
    img_attr = img.attrib
    print (img_attr["src"])

Thereafter, I add the following code to print out the html using pretty_print function

print(etree.tostring(tree, pretty_print=True))

And python return the following

OUTPUT

<html>
  <body>
    <p>GIF89a&#230; &#247;     ))66(??/EE3CC299*,,!&#13;    33&amp;II6\\EiiNooSkkP[[D//#
&#255;&#255;&#255;

##VV@ttV&#140;&#141;i&#138;&#149;y1j&#136;_&#141;&#137;&#150;pddKDD3&amp;&amp;==-]]E&#130;&#130;a&#168;&#168;~&#149;&#171;&#146;+p&#146; R&#142;X&#142;&#150;&#177;&#135;&#134;&#135;cYYB$$__G&#135;&#135;e&#176;&#178;&#134;&#155;&#182;&#160;j&#148; T&#142; S&#142; O&#142;&#144;&#179;&#143;&#161;&#163;wAA0!!XXB&#131;&#131;b&#172;&#175;&#133;&#131;&#168;&#158;[&#143; U&#140;&#13;Z&#140;W&#140; G&#142;&#133;&#175;&#147;&#182;&#184;&#133;zz[KK8''uuW&#157;&#162;}n&#153;&#152;X&#143;4u&#146;&#140;&#177;&#152;_&#150;&#154;M&#144;k&#158;&#144;&#196;&#200;&#146;&#137;&#137;fWWA</p>
  </body>
</html>

It seem python didn't get the correct html code from the link, is there any way I can get the image address? Thanks :)

idjaw
  • 25,487
  • 7
  • 64
  • 83
Ard Wolf
  • 11
  • 3

0 Answers0