Reputation: 15
The code saves a list of URLs. I want to take the lines of text and covert them to links within an HTML file by adding the A tags and place those links within properly formatted HTML code.
#!/usr/bin/env python
import sys
import os
import shutil
try:
from googlesearch import search
except ImportError:
print("No module named 'google' found")
#keyword query user input
query = raw_input('Enter keyword or keywords to search: ')
#print results from search into a file called keyword.txt
with open("keyword.txt","w+") as f:
for j in search(query, tld='co.in', lang='en', num=10, start=0, stop=200, pause=3):
f.write("%s\n" % j)
f.close()
#add keyword to list of keywords file
sys.stdout=open("keywords","a+")
print (query)
sys.stdout.close()
#rename file to reflect query input
os.rename('keyword.txt',query + ".txt")
#move created data file to proper directory and cleanup mess
source = os.listdir("/home/user/search/")
destination = "/home/user/search/index/"
for files in source:
if files.endswith(".txt"):
shutil.copy(files,destination)
os.remove(query + ".txt")
Expected results would be an HTML file with clickable links
Upvotes: 1
Views: 1572
Reputation: 5794
Based on your comment, it appears that you are struggling to write the url string obtained from the search
function into a file along with the required HTML tags. Try:
with open("keyword.txt","w+") as f:
for j in search(query, tld='co.in', lang='en', num=10, start=0, stop=200, pause=3):
f.write('<a href="{0}">{1}</a> <br>\n'.format(j,j))
Which will write each url and add hyperlinks to the url. You might want to print <html> ... </html>
and <body> ... </body>
tags as well to keyword.txt
. This can be done like
with open("keyword.txt","w+") as f:
f.write('<html> \n <body> \n')
for j in search(query, tld='co.in', lang='en', num=10, start=0, stop=200, pause=3):
f.write('<a href="{0}">{1}</a> <br>\n'.format(j,j))
f.write('\n</body> \n </html>')
And, you don't have to close the file using f.close()
if you use with open
see: https://stackoverflow.com/a/8011836/937153
Personally, I prefer format
over %
. I will be careful about presenting a comparison between the two here. You can see Python string formatting: % vs. .format for a detailed discussion on this topic.
Upvotes: 1