How to scrape images from a website and display them on html file?

2024/7/5 12:13:17

I am scraping images from https://www.open2study.com/courses I got all the image sources but dont know how to display the images (instead of links) on a table with 2 column ( one column for title and one for image) on a html file.Can expert help me out?

import urllib
from bs4 import BeautifulSouptitles = []
images = []r = urllib.urlopen('https://www.open2study.com/courses').read()
soup = BeautifulSoup(r)for i in soup.find_all('div', {'class': "courses_adblock_rollover"}):titles.append(i.h2.text)for i in soup.find_all('img', {'class': "image-style-course-logo-subjects-block"}):images.append(i.get('src'))with open('test.txt', "w") as f:for i in zip(titles, images):f.write(i[0].encode('ascii', 'ignore') +'\n'+i[1].encode('ascii', 'ignore') +'\n\n')header = '<!doctyle html><html><head><title>My Title</title></head><body>'
body = '<table><thead><tr><th></th><th></th></tr>'footer = '</table></body></html>'
img_tag = '<img src=,{}">'with open('test.txt', 'r') as input, open('test.html', 'w') as output:output.write(header)output.write(body)for line in input:col1 = line.rstrip().split()col2 = line.rstrip().split()output.write('<tr><td>{}</td><td>{}</td></tr>\n'.format(col1, col2))output.write(footer)
Answer

It was pretty simple problem.try this one

for line in input:#ignore blank linesif line == '\n':continue#why were you spliting here?col1 = line.rstrip()#read next linecol2 = next(input).rstrip()output.write('<tr><td>{}</td><td><img src="{}" style="width: 160px; height: 100px"></td></tr>\n'.format(col1, col2))
https://en.xdnf.cn/q/120328.html

Related Q&A

Multiple files comparing using python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

parsing interactive broker fundamental data

Ive successfully pulled data from IB using the api. It comes in XML format and it looks like this...<TotalRevenues currency="USD"><TotalRevenue asofDate="2017-12-31" report…

How to format HTTP request to discord API?

While this code works, it sends "Bad Request". import socket, ssl token = NzMyMzQ1MTcwNjK2MTR5OEU3.XrzQug.BQzbrckR-THB9eRwZi3Dn08BWrM HOST = "discord.com" PORT = 443 t = POST / HTTP…

Python 3.30 TypeError: object of type int has no len() [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Is it possible to disable negative indexing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Google Colab Notebook completely freezes when training a YOLO model

I am following a tutorial to train custom object detection model using YOLO. This is the tutorial and also where I got the Notebook Everything works fine until the training bit which is the last cell. …

Generate 4 columns of data such that each row sum to 100

How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?

Nan to Num Python

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a NaN. I know that to convert an array where there are numbers that are NaN you…

Class constructor able to init with an instance of the same class object

Can python create a class that can be initialised with an instance of the same class object?Ive tried this:class Class(): def __init__(self,**kwargs):print selfself = kwargs.get(obj,self)print selfif …

Python Logical Operators

I am currently learning Python3, but I dont understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that…