程序跑一次,可以去指定网页,转存一个har data文件。(这是没问题的)
但是,现在要跑1000个网页,读CSV里面的urls,但是始终无法自动命名存储下来的文档。
例如:
CSV:
1. google.com ----> 存:a1.har
2. qq.com ----->存:a2.har
3. 163.com ------> 存:a3.har..(文件名要自动生成后缀,不要覆盖了)
...如此类推 --------> 存:a1000.har
网络上,国内外的网站都找了,确实比较少关于HAR data 的存储。
个人觉得是查了一个循环的语句,让Python直到浏览器,第一个网站进去了,存好了,再去第二个网站,存好了。这样逻辑,
可是能力不够,麻烦大神帮帮忙。
import csv
from browsermobproxy import Server
from selenium import webdriver
import json
server = Server("D:\\browsermob-proxy-2.1.4\\bin\\browsermob-proxy.bat")
server.start()
proxy = server.create_proxy()
profile = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
extension_dir = "C:\\Program Files (x86)\\Mozilla Firefox\\browser\\features\\"
# remember to include .xpi at the end of your file names
extensions = [
'adblock_plus-3.5-an+fx.xpi'
]
for extension in extensions:
driver.install_addon(extension_dir + extension, temporary=True)
proxy.new_har("qq")
path = "D:\websites.csv"
csvfile = open(path,'r')
reader = csv.reader(csvfile)
result = {}
for item in reader:
if reader.line_num ==1:
continue
result[item[0]] = item[1]
csvfile.close()
print(result)
for id, web in result.items():
website = web
driver.get(website)
server.stop()
driver.quit()
f = os.fdopen(fi, "w")