该代码保存URL列表。我想通过添加A标签来将文本行隐藏到HTML文件中的链接,并将这些链接放置在格式正确的HTML代码中。
#!/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")
预期结果将是带有可点击链接的HTML文件
答案 0 :(得分:1)
根据您的评论,您似乎正在努力将从left join stand s1 on s1.stand_id = t.end_point and s1.state = 2
函数获得的url字符串与所需的HTML标记一起写入文件。试试:
select t.id, t.start_point, s.name, t.end_point, s1.name
from transaction t
left join stand s on s.stand_id = t.start_point and s.state = 2
left join stand s1 on s1.stand_id = t.end_point and s1.state = 2
where t.state = 2 and date(t.created_at) = curdate()-1
and t.start_point is not null and t.end_point is not null
将编写每个URL并将超链接添加到该URL。您可能还希望将search
和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))
标签打印到<html> ... </html>
。可以像
<body> ... </body>
而且,如果您使用keyword.txt
,则不必使用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>')
关闭文件,请参见:https://stackoverflow.com/a/8011836/937153
我个人更喜欢f.close()
胜过with open
。在这里将两者进行比较时,我会很小心。您可以在Python string formatting: % vs. .format中找到有关此主题的详细讨论。