Background:
I'm making a script in python which is just a voice assistant (Also has a GUI using Tkinter). I thought it would be fun to have a search option, where you say "search" or "what is" + variable, and it returns it by reading a Wikipedia summary. The problem is, when I pass the command, I get a whole bunch of exceptions:
`
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\u\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py", line 971, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\u\Desktop\wingman\main.py", line 45, in start
results = wikipedia.suggest(phrase)
File "C:\Users\u\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\wikipedia\util.py", line 28, in __call__
ret = self._cache[key] = self.fn(*args, **kwargs)
File "C:\Users\u\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\wikipedia\wikipedia.py", line 180, in suggest
raw_result = _wiki_request(search_params)
File "C:\Users\u\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\wikipedia\wikipedia.py", line 742, in _wiki_request
return r.json()
File "C:\Users\u\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
`
Attempted fix:
I tried fixing this by setting a defined string it searches up when I say "what is". However, even that didn't work. I thought maybe it was because they didn't have an article on it or because there were more articles than just one that correlated with the string, so I changed it to something straight up: Brazil. It should give a summary of the article "Brazil", but it didn't, it still gave me the same exceptions.
Question:
What am I doing wrong? Is it because of the speech recognition module,is it an issue on the computer's side, or is it an issue with the Wikipedia module?
Related
Iniciado!
[WDM] - Downloading: 19.0kB [00:00, 19.5MB/s]
Traceback (most recent call last):
File "main.py", line 58, in
FIREFOX(login)
File "main.py", line 26, in FIREFOX
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
File "C:\Users\moonl\AppData\Local\Programs\Python\Python38\lib\site-packages\webdriver_manager\firefox.py", line 37, in install
driver_path = self.get_driver_path(self.driver)
File "C:\Users\moonl\AppData\Local\Programs\Python\Python38\lib\site-packages\webdriver_manager\core\manager.py", line 26, in get_driver_path
binary_path = self.driver_cache.find_driver(driver)
File "C:\Users\moonl\AppData\Local\Programs\Python\Python38\lib\site-packages\webdriver_manager\core\driver_cache.py", line 101, in find_driver
metadata = self.get_metadata()
File "C:\Users\moonl\AppData\Local\Programs\Python\Python38\lib\site-packages\webdriver_manager\core\driver_cache.py", line 135, in get_metadata
return json.load(outfile)
File "C:\Users\moonl\AppData\Local\Programs\Python\Python38\lib\json_init.py", line 293, in load
return loads(fp.read(),
File "C:\Users\moonl\AppData\Local\Programs\Python\Python38\lib\json_init.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\moonl\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\moonl\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
enter image description here
i am trying to start a selenium program but i get this error
Its very unclear, but seems that is libary error. We need see a part of your code that points to json.
See that if you have the:
import json
If you trying to read a txt file as json, try:
with open('/xxxx/xxxxx/xxxx.xxx') as jsonfile:
data = json.load(jsonfile)
Other topic in stackoverflow can help you, please check the awnser in:
Why am I getting the error: "JSONDecodeError: Expecting value: line 1 column 1 (char 0)" after iteration 28?
PS: One sugestion, try put Try and Catch in your code and put in some blocks, this help see where the error is on the code. One example is:
try
if xxx.......
............
Finnaly
print some error
I'm trying to store the data published on a topic to a JSON file, but I keep getting a JSONDecodeError.
DB = '/home/path/data.json'
f = open(DB, 'w+')
json_array = json.load(fp=f)
json_array.append(data)
json.dump(json_array, f)
f.close()
The open() command successfully creates the file. But loading fails. I have tried running it with the file contents: [] and {}, both gave the same Exception:
[ERROR] [1637699609.562673]: bad callback: <function callback at 0x7fe36196b1f0>
Traceback (most recent call last):
File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 750, in _invoke_callback
cb(msg)
File "/home/path/scripts/extract_info_node.py", line 43, in callback
json_array = json.load(fp=f)
File "/usr/lib/python3.8/json/__init__.py", line 293, in load
return loads(fp.read(),
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
It's not really descriptive and I have nothing to go on. Is there something about the way subscribers are run that doesn't allow writing? Can you even do this from a callback function? I have just started working in ROS so it could be something simple or obvious to someone more experienced.
.load() takes a .read() supporting file. You're only opening the file for reading. Instead try this:
f = open(DB, 'r+')
json_array = json.load(f)
As another note, if you're storing and re-reading topic data I'd suggest looking potentially using rosbag. This is, however, dependent on your actual application.
I want to get information from a forum, Dcard.
I have also built a function to arrange the data I receive.
import json
import pandas as pd
import requests
from requests_html import HTML
import re
import openpyxl
def Crawl(ID):
link='https://www.dcard.tw/_api/posts/'+str(ID)
requ=requests.get(link)
rejs=requ.json()
return(pd.DataFrame(
data=
[{'ID':rejs['id'],
'title':rejs['title'],
'content':rejs['content'],
'excerpt':rejs['excerpt'],
'createdAt':rejs['createdAt'],
'updatedAt':rejs['updatedAt'],
'commentCount':rejs['commentCount'],
'forumName':rejs['forumName'],
'forumAlias':rejs['forumAlias'],
'gender':rejs['gender'],
'likeCount':rejs['likeCount'],
'reactions':rejs['reactions'],
'topics':rejs['topics']}],
columns=['ID',"title","content","excerpt","createdAt","updateAt","commentCount",
"forumName","forumAlias","gender","likeCount","reactionns","topics"])
)
test3=open('D:/Quinna/爬蟲/test4.txt','w',encoding='UTF-8')
p=requests.Session()
url=requests.get("https://www.dcard.tw/search?query=%E5%88%86%E6%89%8B&forum=relationship")
soup=BeautifulSoup(url.text,"html.parser")
a_tags=soup.find_all('h2')
for tag in a_tags:
a=tag.select_one('a').get('href')[18:27] #get the ID from the API
print(Crawl(a))
However, whenever I tried, the error message still comes up.
Is the json not a valid format? How to amend it?
Traceback (most recent call last):
File "D:\Quinna\爬蟲\test4.py", line 38, in <module>
print(Crawl(a))
File "D:\Quinna\爬蟲\test4.py", line 12, in Crawl
rejs=requ.json()
File "C:\Users\user\AppData\Roaming\Python\Python38\site-packages\requests\models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Program Files (x86)\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Program Files (x86)\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files (x86)\Python38-32\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
This this. You aren't getting the IDs in a proper way.
Replace this line:
a = tag.select_one('a').get('href')[18:27]
to this line:
a = tag.select_one('a').get('href').split('/')[-1]
I've been trying to process a couple thousand requests in python and then write them to a json file. For some reason whenever I run my code, I get the following error:
File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver\_sandbox.py", line 133, in <module>
File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver\_sandbox.py", line 44, in main
File "E:\Python Programs\API Challenge Aug 2015\API Challenge Files\Execute Requests Files\RiotAPI.py", line 112, in get_match_by_matchid
return self._request(api_url, region)
File "E:\Python Programs\API Challenge Aug 2015\API Challenge Files\Execute Requests Files\RiotAPI.py", line 58, in _request
return response.json()
File "C:\Python\Lib\site-packages\requests\models.py", line 819, in json
return json.loads(self.text, **kwargs)
File "C:\Python\Lib\json\__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "C:\Python\Lib\json\decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python\Lib\json\decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
builtins.ValueError: Expecting value: line 1 column 1 (char 0)
Note that the error will occur after what is a seemingly random number of requests. Sometimes it'll happen after under 10 requests, sometimes it'll last for a couple hundred requests before the error occurs. I've exhaustively tried this and it hasn't broken after the same number of requests once.
The files I'm using that cause this error are identical in method to the post you can see here: What am I doing wrong with requests in python: ValueError: Expecting value: line 1 column 1 (char 0)?
I believe my error comes from the same issue you'll find in that thread, but as far as I can tell and answer was never found. Can anyone help me with this issue?
I am receiving the following error when running the below python script.
ValueError: Invalid control character at: line 7591 column 220620 (char 385678)
I did some research on this and it appeared that it would be resolved by passing 'strict=false' within json.dumps(), but I'm still receiving the same error. This is the only REST service that I have attempted to query that returns this error.
import arcgis
import json
from arcgis import ArcGIS
service = ArcGIS("http://mapping.dekalbcountyga.gov/arcgis/rest/services/LandUse/MapServer")
query = service.get(0, count_only=False)
json_query = json.dumps(query, strict=False)
f = open("dekalb_parcels.geojson", "w")
f.write(json_query)
f.close()
Any help that can be provided would be very appreciated. Thank you.
UPDATE - This is the full error that I am receiving.
Traceback (most recent call last):
File "G:\Python\Scripts\dekalb_parcel_query.py", line 8, in <module>
query = service.get(0, count_only=False)
File "C:\Python27\lib\site-packages\arcgis\arcgis.py", line 146, in get
jsobj = self.get_json(layer, where, fields, count_only, srid)
File "C:\Python27\lib\site-packages\arcgis\arcgis.py", line 90, in get_json
return response.json()
File "C:\Python27\lib\site-packages\requests\models.py", line 802, in json
return json.loads(self.text, **kwargs)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid control character at: line 7591 column 220620 (char 385678)
I was able to fix this issue by passing strict=False within return response.json()