I am trying to get some google images through python, However when I try to read the json response from the server i am getting an error telling me that the object must be str instead of bytes.
As a matter of fact, I tried to solve the issue by converting the respond of the server to str by decoding it ( utf-8). I am getting another error telling me "Expecting value: line 1 column 1 (char 0)" Here is what I have done
1. I search for BLACK SHIRTS on google and clicked on image rubric. I copied the URL.
2. I create headers where I inform that the user-agent is chrome (in order not to get blocked when sending the request to the server )
3. I create a request
4. I read the request
5. when i try to load it I got the issue mentioned above. So i tried to decode the output of step 4 ( by adding this .decode('utf-8') .
我试图通过python获取一些谷歌图像,但是当我尝试从服务器读取json响应时,我收到一个错误,告诉我该对象必须是str而不是字节。事实上,我尝试通过解码服务器将响应转换为str来解决问题(utf-8)。我收到另一个错误告诉我“期望值:第1行第1列(字符0)”这是我所做的1.我在谷歌搜索黑色衬衫并点击图像标记。我复制了网址。 2.我创建标题,我告知用户代理是chrome(为了在向服务器发送请求时不被阻止)3。我创建请求4.我读取请求5.当我尝试加载它时我得到了上面提到的问题。所以我试图解码步骤4的输出(通过添加这个.decode('utf-8')。
Here is the code that I used
这是我使用的代码
import os
import urllib.request as ulib
import urllib.parse
import json
headers = {'User-Agent': 'Chrome/41.0.2228.0 Safari/537.36'}
url = 'https://www.google.com/search?q=BLACK+SHIRTS&source=lnms&tbm=isch&sa=X&ved=0ahUKEwiS2NPpxPbbAhWlMewKHSpiC9IQ_AUICygC&biw=1600&bih=794' ## this is the url when i searched
request = ulib.Request(url, None, headers)
json_string = ulib.urlopen(request).read()
json_string=json_string.decode('utf-8')
page = json.loads(json_string)
PS: remove .decode('utf-8') from the 4th step ( that matchs the line before last )
PS:从第4步(与前一行匹配)中删除.decode('utf-8')
1 个解决方案
#1
0
You need to parse the HTML response with a package like Beautiful Soup https://www.crummy.com/software/BeautifulSoup/?
您需要使用像Beautiful Soup这样的软件包解析HTML响应https://www.crummy.com/software/BeautifulSoup/?
#1
0
You need to parse the HTML response with a package like Beautiful Soup https://www.crummy.com/software/BeautifulSoup/?
您需要使用像Beautiful Soup这样的软件包解析HTML响应https://www.crummy.com/software/BeautifulSoup/?