1

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

2
  • Wouldn't it be sufficient to print the required HTML tags along with what you are printing right now? Commented Apr 26, 2019 at 0:55
  • When I try: f.write('atag' "%s\n" % j ("%s\n" % j, 'btag')) I get this error: TypeError: 'unicode' object is not callable Commented Apr 26, 2019 at 5:14

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have altered the code since then, however you have certainly helped me solve this problem. Thank you very much.
Glad I could help :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.