1.urllib.request: 发送HTTP请求,接收处理HTTP响应 2.urllib.error: 处理错误和异常,包含urllib.request抛出的异常 3.urllib.parse: 解析URL和编码数据 4.urllib.robotparser: 解析robots.txt 本文介绍urllib库中的request, error, parse三个模块在HTTP开发时的代码应用,并封装成http_util,单元测试函数实际调用百度AI...
POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str. 另一个同类错误: can't concat str to bytes 解决:调用urllib.parse.urlencode()将数据编码,或者调用封装的http_util.encode() 原因:urllib.request.Request()的data参数类型要求byte...
TypeError: can’t concat str to bytes 通过分析可以发现 data参数应该是bytes类型的,而我们创建的form_data字典是string类型的,这时候我们就需要用urllib包中的urllib.parse库,并运用其中的函数urlencode() urllib.parse.urlencode(query, doseq=False, safe=’’, encoding=None, errors=None, quote_via=...
File "D:\anaconda3\Install\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) TypeError: can't concat str to bytes 因为我们没给data编码,我们先把str变为Unicode再转为bytes二进制码,然后发送 req = request.Request(url, headers=headers, data=parse.urlencode(da...
搜索指定字符串,没有返回-1:str.find('t')python字符串查找指定多个子字符串的所有位置:a = "dhvka,feovj.dlfida?dfka.dlviaj,dlvjaoid,vjaoj?"b = [i for i, j in enumerate(a) if j in [',', '.', '?']]print(b)[5, 11, 18, 23, 30, 39, 45]...
#!/usr/bin/python3 str = input("请输入:"); print ("你输入的内容是: ", str) 这会产生如下的对应着输入的结果: 请输入:Hello Python! 你输入的内容是: Hello Python! Print()输出:print 默认输出是换行的,如果要实现不换行需要在变量末尾加上 end="": ...
import urllib.parse def parse(self, response): items = Selector(response).xpath('//*[@id="content-list"]/div[@class="item"]') for item in items: news = item.xpath( './div[@class="news-content"]' '//a[contains(concat(" ", normalize-space(@class), " "), " show-content "...
python-m webbrowser-t https://www.zd200572.com 4.10 OS模块处理目录和文件 直接看最简单的代码啦! 4.10.1 创建与删除文件目录 代码语言:javascript 代码运行次数:0 运行 AI代码解释 importos directory_name='abcd'print('Creating',directory_name)# 创建文件夹 ...
The minimum required version of urllib3 has been bumped to 1.26.18 to address a security vulnerability. The minimum required version of shapely has been bumped to 2.0.0 to address thread safety issues. Python 3.7, formerly deprecated, is no longer supported. Python 3.12 is not yet officially ...
import_example_package_urllib.py import_example_pep.py import_example_relative.ipynb import_example_relative.py import_example_relative_other.ipynb import_example_relative_other.py in_basic.ipynb in_basic.py in_timeit.ipynb in_timeit.py indentation_usage.ipynb indentation_usage.py...