I have gone through very similar stackoverflow page: Python download image with lxml but still that did not work for my case.
I would like to get some help downloading images from CNN business forecast page. Here is my code so far:
import lxml.html
import requests
ticker = "AAL"
ticker = ticker.upper()
url = f"https://money.cnn.com/quote/forecast/forecast.html?symb={ticker}"
xpath = '//*[@id="wsod_forecasts"]/div[1]/div/img'
response = requests.get(url)
parsed_page = lxml.html.fromstring(response.content) # this gives a list
# from: https://stackoverflow.com/questions/11566596/python-download-image-with-lxml
# this also fails
tree = lxml.html.parse(url)
img = tree.get_element_by_id('img')
img_url = img.attrib['src']
with open('image.jpg', 'wb') as outf:
data = requests.get(img_url).content
outf.write(data)
How to download the image?
After your parsed_page
add:
img_url = "http:"+parsed_page.xpath('//*[@id="wsod_forecasts"]/div[1]/div/img')[0].attrib['src']
or alternatively:
img_url = "http:"+parsed_page.xpath('//*[@id="wsod_forecasts"]//img')[0].attrib['src']
and then run your with open()
and it should download.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.