简体   繁体   中英

How to import Python variables found from a website found using regular expression findall (E.g. product name, picture, price) into a HTML document?

Hi guys i'm trying to figure out how to import variables that I have saved into lists into a HTML document.

##DOG ITEMS
dog_url = 'https://feed.zazzle.com/rss?qs=dogs'
dog_contents = urlopen(dog_url).read()
###DOG ITEM INFO
dog_names = findall('<title>[<![]+CDATA[[]([A-Za-z0-9 ]+)' 

I want to be able to post the list data into the HTML document based on its index. So dog_names[0] = "item 1" posted as text.

This is my HTML code.

file_name = open('invoice.html','w')
message = """<html>

<head>
<center>
<h1 style="font-size:45px;">BAD BUSINESS PLAN PTY LTD. INVOICE</h1>
<img src="companyLogo.jpg" width="400" height ="400" alt="Company Logo">
</center>
</head>

<body>
<p><table border="2"; align="center"><td>%dog_names</td></table></p>
</body>

</html>"""

file_name.write(message)
file_name.close()

Any help would be greatly appreciated!!!

checkout pythons format syntax . You can use it for your string output as follows:

message = """<html>

<head>
<center>
<h1 style="font-size:45px;">BAD BUSINESS PLAN PTY LTD. INVOICE</h1>
<img src="companyLogo.jpg" width="400" height ="400" alt="Company Logo">
</center>
</head>

<body>
<p><table border="2"; align="center"> {dog_names} </table></p>
</body>

</html>"""

line_template = "<tr><td> {dog_name} </td></tr>"

dog_name_list = [ line_template.format(dog_name = dog_name) for dog_name in dog_names ]
dog_names = "".join(dog_name_list)

print message.format(dog_names=dog_names)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM