我目前正在运行一个python脚本来检测网站的json对象中包含产品的变化。
当我在我的ubuntu服务器上手动运行它工作正常(发送推文)但是当它与crontab一起运行时,它给了我这个错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)
这是代码。基本上我正在比较不同版本的json文件,看看是否有区别。如果有差异,请发送推文。
#!/usr/bin/env python3
import twitter
import requests
from jsondiff import diff
from classes.logger import Logger
from classes.proxies import Proxy
import webbrowser
import time
import json
log = Logger().log
class Cactus:
proxy = Proxy()
def __init__(self):
self.url = 'http://api.bigcartel.com/cactusplantfleamarket/products.json'
self.front = 'http://www.cactusplantfleamarket.bigcartel.com'
self.api = twitter.Api(consumer_key='xxx',
consumer_secret='xxx',
access_token_key='xxx',
access_token_secret='xxx')
def scrape(self):
with open('cactus.txt') as oldjson:
old = json.load(oldjson);
current_proxy = self.proxy.getProxy()[self.proxy.countProxy()]
session = requests.session()
resp = session.get(self.url, proxies=current_proxy).json()
with open('cactus.txt', 'w') as outfile:
json.dump(resp, outfile)
if diff(resp, old) != {}:
if len(resp) == 0:
curr_time = time.strftime("%d %b %H:%M:%S", time.gmtime())
self.api.PostUpdate('Website Updated at ' + curr_time )
else:
for item in range(len(resp)):
try:
self.tweet(resp[item])
except Exception as e:
print(e)
def tweet(self, item):
print(item['name'])
curr_time = time.strftime("%d %b %H:%M:%S", time.gmtime())
url = self.front + item['url']
shorturl = self.goo_shorten_url(url)['id']
self.api.PostUpdate('CPFM: ' + item['name'] + ' - ' + curr_time + ' ' + shorturl)
def goo_shorten_url(self, url):
API_KEY = 'xxxx'
post_url = 'https://www.googleapis.com/urlshortener/v1/url?key={}'.format(API_KEY)
payload = {'longUrl': url}
headers = {'content-type': 'application/json'}
r = requests.post(post_url, data=json.dumps(payload), headers=headers)
return r.json()
答案 0 :(得分:0)
我们在聊天中做了一些调查,发现脚本是用Python 2而不是Python 3意外运行的(线索是u
上的u'\xa0'
前缀。)