【问题标题】:how to save the output files in new folder?? now these are stored in root. guide me to save in specific location and i wanted to give file name如何将输出文件保存在新文件夹中?现在这些存储在根目录中。指导我保存在特定位置,我想给出文件名
【发布时间】:2021-01-18 16:03:04
【问题描述】:

请帮我升级以下内容:这实际上是打开网站并截图。

我需要将 o/p 文件保存在所需位置。如果可能的话,请帮我取名字。

示例:如果输入为:https://www.amazon.in/B078BNQ318/ 我希望将文件保存为 B078BNQ318。请帮忙

import webbrowser
import pyautogui, time
import xlrd

URL = []
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# change the loc as per your excel location
loc = ("C:\\Users\\dilip\\Documents\\URL.xlsx")
wb = xlrd.open_workbook(loc)
# open the first sheet
sheet = wb.sheet_by_index(0)

for i in range(sheet.nrows):
    URL.append(sheet.cell_value(i, 0))

for i in range(0, len(URL)):
    webbrowser.get(chrome_path).open(URL[i])
    time.sleep(5)
    screenshot = pyautogui.screenshot()
    filename = "file" + str(i) + ".png"
    screenshot.save(filename)

【问题讨论】:

    标签: python python-3.x web-scraping python-requests


    【解决方案1】:

    我确定您可以在路径上使用 split 并获取最后一个元素。 代码我看不懂,就说我懂的吧

    #assume this is the url
    
    url = 'https://www.amazon.in/B078BNQ318/'
    i = url.split('/')
    
    #assuming all url's are like the example you gave 
    
    filename = "file" + str(i[-2]) + ".png"
    
    #output of i = ['https:', '', 'www.amazon.in', 'B078BNQ318','']
    

    【讨论】:

      【解决方案2】:

      你可以在python中使用f字符串。

      for i in range(0, len(URL)):
          webbrowser.get(chrome_path).open(URL[i])
          time.sleep(5)
          screenshot = pyautogui.screenshot()
          filename = "file" + str(i) + ".png"
          screenshot.save(f"D:\\{filename}")   # Here I gave D drive as location.Change this acccording to your need
      

      【讨论】:

      • SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
      猜你喜欢
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多