我正在使用此代码从外部程序获取标准输出:
>>> from subprocess import *
>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
Run Code Online (Sandbox Code Playgroud)
communic()方法返回一个字节数组:
>>> command_stdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2\n'
Run Code Online (Sandbox Code Playgroud)
但是,我想将输出作为普通的Python字符串.所以我可以这样打印:
>>> print(command_stdout)
-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2
Run Code Online (Sandbox Code Playgroud)
我认为这是binascii.b2a_qp()方法的用途,但是当我尝试它时,我又得到了相同的字节数组:
>>> binascii.b2a_qp(command_stdout)
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2\n'
Run Code Online (Sandbox Code Playgroud)
有人知道如何将字节值转换回字符串吗?我的意思是,使用"电池"而不是手动操作.而且我希望它能用于Python 3.
所以,我有这个代码:
url = 'http://google.com'
linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')
m = urllib.request.urlopen(url)
msg = m.read()
links = linkregex.findall(msg)
Run Code Online (Sandbox Code Playgroud)
但是然后python返回这个错误:
links = linkregex.findall(msg)
TypeError: can't use a string pattern on a bytes-like object
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我正在尝试使用正则表达式搜索网页,但我收到以下错误:
TypeError:不能在类字节对象上使用字符串模式
我理解为什么,urllib.request.urlopen()返回一个字节流,所以,至少我猜测,重新不知道要使用的编码.在这种情况下我该怎么办?有没有办法在urlrequest中指定编码方法,或者我需要自己重新编码字符串?如果是这样我想要做什么,我假设我应该从头信息或编码类型中读取编码,如果在html中指定,然后重新编码为它?
只是试图测试非常简单的Python JSON命令,但我遇到了一些麻烦.
urlopen('http://www.similarsitesearch.com/api/similar/ebay.com').read()
Run Code Online (Sandbox Code Playgroud)
应该输出
'{"num":20,"status":"ok","r0":"http:\\/\\/www.propertyroom.com\\/","r1":"http:\\/\\/www.ubid.com\\/","r2":"http:\\/\\/www.bidcactus.com\\/","r3":"http:\\/\\/www.etsy.com\\/","r4":"http:\\/\\/us.ebid.net\\/","r5":"http:\\/\\/www.bidrivals.com\\/","r6":"http:\\/\\/www.ioffer.com\\/","r7":"http:\\/\\/www.shopgoodwill.com\\/","r8":"http:\\/\\/www.beezid.com\\/","r9":"http:\\/\\/www.webidz.com\\/","r10":"http:\\/\\/www.auctionzip.com\\/","r11":"http:\\/\\/www.overstock.com\\/","r12":"http:\\/\\/www.bidspotter.com\\/","r13":"http:\\/\\/www.paypal.com\\/","r14":"http:\\/\\/www.ha.com\\/","r15":"http:\\/\\/www.onlineauction.com\\/","r16":"http:\\/\\/bidz.com\\/","r17":"http:\\/\\/www.epier.com\\/","r18":"http:\\/\\/www.sell.com\\/","r19":"http:\\/\\/www.rasmus.com\\/"}'
Run Code Online (Sandbox Code Playgroud)
但我得到了相同的字符串,b前面有一个字符串:
b'{"num":20,"status":"ok","r0":"http:\\/\\/www.propertyroom.com\\/","r1":"http:\\/\\/www.ubid.com\\/","r2":"http:\\/\\/www.bidcactus.com\\/","r3":"http:\\/\\/www.etsy.com\\/","r4":"http:\\/\\/us.ebid.net\\/","r5":"http:\\/\\/www.bidrivals.com\\/","r6":"http:\\/\\/www.ioffer.com\\/","r7":"http:\\/\\/www.shopgoodwill.com\\/","r8":"http:\\/\\/www.beezid.com\\/","r9":"http:\\/\\/www.webidz.com\\/","r10":"http:\\/\\/www.auctionzip.com\\/","r11":"http:\\/\\/www.overstock.com\\/","r12":"http:\\/\\/www.bidspotter.com\\/","r13":"http:\\/\\/www.paypal.com\\/","r14":"http:\\/\\/www.ha.com\\/","r15":"http:\\/\\/www.onlineauction.com\\/","r16":"http:\\/\\/bidz.com\\/","r17":"http:\\/\\/www.epier.com\\/","r18":"http:\\/\\/www.sell.com\\/","r19":"http:\\/\\/www.rasmus.com\\/"}'
Run Code Online (Sandbox Code Playgroud)
随后,当我试着跑
json.loads(urlopen('http://similarsitesearch.com/api/similar/ebay.com').read())
Run Code Online (Sandbox Code Playgroud)
它给了我错误信息:
TypeError:不能在类字节对象上使用字符串模式"
我假设与之有关b?
我urlopen从中导入urllib.request,我正在运行Python 3.
有任何想法吗?
import json
import requests
url = 'http://developer.usa.gov/1usagov.json'
r = requests.get(url, stream=True)
for line in r.iter_lines():
if line:
print (json.loads(line))
Run Code Online (Sandbox Code Playgroud)
给出了这个错误:
TypeError: can't use a string pattern on a bytes-like object
Run Code Online (Sandbox Code Playgroud)
通过浏览器查看时,我确实看到响应是一个Json,但请求库说它像对象一样的字节为什么呢?
我正在进行python挑战并试图熟悉python,所以没有看到答案,我尝试使用python的url阅读器来阅读html,然后找到所需的字母.但是在下面的代码中我得到一个错误,最初是python 3 urllib.request但是在解析之后我得到一个新的错误:
Run Code Online (Sandbox Code Playgroud)<module> print ("".join(re.findall("[A-Za-z]", data))) File "C:\Python34\lib\re.py", line 210, in findall return _compile(pattern, flags).findall(string) TypeError: can't use a string pattern on a bytes-like object
现在我试着在谷歌上看这个错误,但我得到的只是关于json,我不应该需要它?我的python并不那么强大,所以也许我这样做不正确?
#Question 2 - find rare characters
import re
import urllib.request
data = urllib.request.urlopen("http://www.pythonchallenge.com/pc/def/ocr.html")
mess = data.read()
messarr = mess.split("--")
print ("".join(re.findall("[A-Za-z]", data)))
#Question 3 - Find characters in list
page = urllib.request.urlopen("http://www.pythonchallenge.com/pc/def/equality.html")
mess = page.read()
messarr = mess.split("--")
print ("".join(re.findall("[^A-Z]+[A-Z]{3}([a-z])[A-Z]{3}[^A-Z]+", page)))
Run Code Online (Sandbox Code Playgroud) import urllib.request
import re
f = urllib.request.urlopen('http://www.geekynu.cn/')
html = f.read()
title = re.search('<title>(.*?)</title>', html)
print(title)
#print(title.decode('utf-8')) //I had try to solve by this code.
Run Code Online (Sandbox Code Playgroud)
[python 3.5] 当我使用re.search()阅读网页标题时,出现错误“TypeError: cannot use a string pattern on a bytes-like object”,我该怎么办?谢谢!
我想建立一个网络刮板.目前,我正在学习Python.这是非常基础!
Python代码
import urllib.request
import re
htmlfile = urllib.request.urlopen("http://basketball.realgm.com/")
htmltext = htmlfile.read()
title = re.findall('<title>(.*)</title>', htmltext)
print (htmltext)
Run Code Online (Sandbox Code Playgroud)
错误:
File "C:\Python33\lib\re.py", line 201, in findall
return _compile(pattern, flags).findall(string)
TypeError: can't use a string pattern on a bytes-like object
Run Code Online (Sandbox Code Playgroud) 我正在使用 Python RegEx 显示所有连接到计算机的互联网无线配置文件(TypeError: cannot use a string pattern on a bytes-like object)。我的第二行有错误,请任何人帮助识别我的错误。谢谢
我的程序
import subprocess,re
command = "netsh wlan show profile"
output = subprocess.check_output(command, shell=True)
network_names = re.search("(Profile\s*:\s)(.*)", output)
print(network_names.group(0))
Run Code Online (Sandbox Code Playgroud)
………………………………………………………………………………………………………………………………………………………… ...
错误
line 8, in <module>
return _compile(pattern, flags).search(string)
TypeError: cannot use a string pattern on a bytes-like object
Run Code Online (Sandbox Code Playgroud) 我想使用函数re.findall(),它在网页中搜索某个模式:
from urllib.request import Request, urlopen
import re
url = Request('http://www.cmegroup.com/trading/products/#sortField=oi&sortAsc=false&venues=3&page=1&cleared=1&group=1', headers={'User-Agent': 'Mozilla/20.0.1'})
webpage = urlopen(url).read()
findrows = re.compile('<td class="cmeTableCenter">(.*)</td>')
row_array = re.findall(findrows, webpage) #ERROR HERE
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
TypeError: can't use a string pattern on a bytes-like object
Run Code Online (Sandbox Code Playgroud) 嗨,我在python中编码试图为学校项目制作一个用户友好的货币兑换应用程序,但遇到了错误,试图解码json的汇率.我正在使用的代码是:
import urllib.request
import json
(str) = "http://rate-exchange.appspot.com/currency?from=FRM&to=TO&q=AM";
(str) = (str.replace("FRM", "GBP"))
(str) = (str.replace("TO", "USD"))
url = (str.replace("AM", "20"))
f = urllib.request.urlopen(url)
data = (f.read(100))
print (data)
json_input = data
decoded = json.loads(json_input)
print ("conversion is: ", decoded["v"])
Run Code Online (Sandbox Code Playgroud)
而我得到的错误是:
b'{"to": "USD", "rate": 1.66215, "from": "GBP", "v": 33.243000000000002}'
Traceback (most recent call last):
File "C:\Users\jay\My Cubby\get qure.py", line 12, in <module>
decoded = json.loads(json_input)
File "C:\Python33\lib\json\__init__.py", line 309, in loads
return _default_decoder.decode(s)
File "C:\Python33\lib\json\decoder.py", line 352, in decode
obj, end …Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×6
regex ×4
json ×2
string ×2
bytestring ×1
encoding ×1
findall ×1
python-3.8 ×1
scraper ×1
typeerror ×1
urllib ×1
urlopen ×1
web ×1
web-scraping ×1