收到错误“ pywintypes.error:(5,'OpenClipboard','访问被拒绝。')”

时间:2018-12-03 12:39:10

标签: python-3.x anaconda selenium-chromedriver pywin32

我正在尝试将硒与python结合使用,以从this网站获取经度和纬度。我也在使用win32lipboard。但是,每当我运行我的代码时,它就会随机抛出该错误pywintypes.error: (5, 'OpenClipboard', 'Access is denied.')

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import csv
import win32clipboard

csvreader = csv.reader(open("master_data.csv", 'r'))

csvwriter = csv.writer(open('final_master_data.csv', 'w', newline=''))

headers = next(csvreader)
headers.append("latitude")
headers.append("longitude")
csvwriter.writerow(headers)

locations = list(csvreader)

chromedriver = 'C:\\Users\\UserName\\Downloads\\chromedriver.exe'
driver = webdriver.Chrome(chromedriver)
driver.get('http://www.whatsmygps.com')

for places in locations:
    place = places[6] + ", " + places[4] + ", " + places[2]

    location = driver.find_element_by_id("address")

    location.send_keys(Keys.CONTROL, 'a')
    location.send_keys(place)

    location.submit()

    time.sleep(3)

    lat_input = driver.find_element_by_id("latitude")
    lat_input.send_keys(Keys.CONTROL, 'a')
    lat_input.send_keys(Keys.CONTROL, 'c')

    win32clipboard.OpenClipboard()
    lat = win32clipboard.GetClipboardData()

    places.append(lat)

    win32clipboard.CloseClipboard()

    lon_input = driver.find_element_by_id("longitude")
    lon_input.send_keys(Keys.CONTROL, 'a')
    lon_input.send_keys(Keys.CONTROL, 'c')

    win32clipboard.OpenClipboard()
    lon = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()

    places.append(lon)
    print(places)

    csvwriter.writerow(places)

driver.close()

因此,每当我运行此代码时,它都不会出现任何问题,它将读取csv文件并在该站点中输入位置名称,并开始从该站点复制纬度和经度,并将其插入另一个csv文件中。但是一段时间后,它会随机抛出错误pywintypes.error: (5, 'OpenClipboard', 'Access is denied.')。自昨天以来,我无法找到解决方案。

更新:我正在使用Anaconda,并且以管理员身份运行anaconda shell,因此访问权限没有问题。

1 个答案:

答案 0 :(得分:1)

如果剪贴板被另一个进程锁定,则可能会发生拒绝访问错误。为避免python消息,您可以按照以下SO链接所述使用WinAPI版本的剪贴板:https://stackoverflow.com/a/23285159/4603670


或者,使用需要API密钥的BingMap。在撰写本文时,您可以在https://www.bingmapsportal.com注册一个免费API密钥的开发者帐户(我不确定配额)。

import pythoncom
import win32com.client
import json

pythoncom.CoInitialize()
winhttp = win32com.client.Dispatch('WinHTTP.WinHTTPRequest.5.1')

def bing_find_gps(addressLine, postalCode, country):

    q = 'http://dev.virtualearth.net/REST/v1/Locations?key='
    q = q + 'my_api_key'
    if country: q = q + '&countryRegion=' + country
    if postalCode: q = q + '&postalCode=' + postalCode
    if addressLine: q = q + '&addressLine=' + addressLine

    try:
        winhttp.Open('GET', q, False)
        winhttp.Send()
        if not winhttp.responseText:
            return 0
        list = json.loads(winhttp.responseText)
        if list['statusCode'] != 200:
            return 0
        gps = list['resourceSets'][0]['resources'][0]['point']['coordinates']
        if gps:
            return (1, gps[0], gps[1])
    except:
        return 0

res = bing_find_gps('One Microsoft Way, Redmond, WA, 98052-6399', '0', 'United States')
if res:
    print("lat/long %s, %s" % (res[1], res[2]))
res = bing_find_gps(0, '98052-6399', 'United States')
if res:
    print("lat/long %s, %s" % (res[1], res[2]))


或使用openstreetmap.org

address = "98052-6399" #Testing with Microsoft zip code
url = "https://nominatim.openstreetmap.org/search?format=json&q=" + address
winhttp.Open('GET', url, False)
winhttp.Send()
list = json.loads(winhttp.responseText)
print(list[0].get('lat'))
print(list[0].get('lon'))

预期输出:

Latitude: 47.670119 
Longitude: -122.118237

或者您也可能希望避免完全复制该元素,请使用get_attribute('value')来读取latitudelongitude中的值。示例:

chromedriver = 'C:\\Users\\UserName\\Downloads\\chromedriver.exe'
driver = webdriver.Chrome(chromedriver)
driver.get('http://www.whatsmygps.com')

element = driver.find_element_by_id("address")
element.send_keys(Keys.CONTROL, 'a')
#enter Microsoft's zip code
element.send_keys('98052-6399')
element.submit()
time.sleep(3)

lat_input = driver.find_element_by_id("latitude")
print('latitude: ')
print(lat_input.get_attribute('value'))

lon_input = driver.find_element_by_id("longitude")
print('longitude: ')
print(lon_input.get_attribute('value'))

driver.close()