我有一个单一URL的python脚本,我需要从url.txt为多个URL运行它,并在单个txt文件中获取输出。
这是python脚本(缩小):
import urllib2
from bs4 import BeautifulSoup
quote_page = 'https://www.example.com/page/1024'
#Rest of the script here
print var1
print var2
print var3
以下是一个URL的示例输出:
Name: John Doe
DOB: 01-Jan-1980
Gender: Male
我希望这个输出为URL 1,我的脚本完全按照我的意愿提供。我想对URL 2,URL 3等重复此操作,如url.txt中所示。
有什么想法?
附:我一直把这个问题简单化了,但是如果你需要更多的细节,我知道,我会这样做。
以附加模式打开文件并为其中的每一个写入输出。
import urllib2
from bs4 import BeautifulSoup
quote_page = 'https://www.example.com/page/1024'
#Rest of the script here
output = open("output.txt", 'a') # 'a' means open in append mode so the file is not overwritten
# change print to output.write()
output.write(str(var1) + '\n') # separate each var by a new line
output.write(str(var2) + '\n')
output.write(str(var3) + '\n')
output.close()
这将写入var1的全部,然后是var2的全部,然后是var3的全部,每个都用空行分隔,然后关闭文件。
要使其更兼容以从命令行接受URL:
import sys
import urllib2
from bs4 import BeautifulSoup
quote_page = sys.argv[1] # this should be the first argument on the command line
#Rest of the script here
output = open("output.txt", 'a') # 'a' means open in append mode so the file is not overwritten
# change print to output.write()
output.write(str(var1) + '\n') # separate each var by a new line
output.write(str(var2) + '\n')
output.write(str(var3) + '\n')
output.close()
使用您的网址的示例命令行:
$python3.6 myurl.py https://www.example.com/page/1024
要从文件中获取URL,您需要打开它,然后为每一行运行脚本。假设有一个网址。要写入输出文件,请打开文件并将var1,var2和var3写入其中
import urllib2
from bs4 import BeautifulSoup
with open('url.txt') as input_file:
for url in input_file:
quote_page = url
#Rest of the script here
with open("ouput_file.txt", "w") as output:
output.write(f'{var1}\n')
output.write(f'{var2}\n')
output.write(f'{var3}\n')