学习python爬虫时,出现不知名问题
源代码如下:
askurl("https://movie.douban.com/top250?start=0")
def askurl(url): #模拟浏览器头部信息,向豆瓣服务器发送消息
head={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188"
}
#用户代理,表示告诉豆瓣服务器,我们是什么类型的机器浏览器(本质上是告诉浏览器,我们可以接收什么水品的文件内容)
req = urllib.request.Request(url,headers=head)
html=''
try:
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
print(html)
except urllib.error.URLError as e:
if hasattr(e,'code'):
print(e.code)
if hasattr((e,'reason')):
print(e.reason)
#return html
报错内容如下:
Traceback (most recent call last):
File "D:\软件\pycharm\pythonProject7\爬虫主程序.py", line 54, in <module>
main()
File "D:\软件\pycharm\pythonProject7\爬虫主程序.py", line 19, in main
askurl("https://movie.douban.com/top250?start=0")
File "D:\软件\pycharm\pythonProject7\爬虫主程序.py", line 37, in askurl
response = urllib.request.urlopen(req)
File "D:\软件\python3.10\lib\urllib\request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "D:\软件\python3.10\lib\urllib\request.py", line 519, in open
response = self._open(req, data)
File "D:\软件\python3.10\lib\urllib\request.py", line 536, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "D:\软件\python3.10\lib\urllib\request.py", line 496, in _call_chain
result = func(*args)
File "D:\软件\python3.10\lib\urllib\request.py", line 1391, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "D:\软件\python3.10\lib\urllib\request.py", line 1348, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "D:\软件\python3.10\lib\http\client.py", line 1283, in request
self._send_request(method, url, body, headers, encode_chunked)
File "D:\软件\python3.10\lib\http\client.py", line 1294, in _send_request
self.putrequest(method, url, **skips)
File "D:\软件\python3.10\lib\http\client.py", line 1132, in putrequest
self._output(self._encode_request(request))
File "D:\软件\python3.10\lib\http\client.py", line 1212, in _encode_request
return request.encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\uff1f' in position 11: ordinal not in range(128)
Process finished with exit code 1
现在用requests模块爬虫比较方便,我很少用urllib模块爬虫
# -*- coding:utf-8 -*-
import requests
def askurl(url): # 模拟浏览器头部信息,向豆瓣服务器发送消息
head = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188"
}
html = requests.get(url,headers=head)
result = html.content.decode('utf-8')
print(result)
if __name__ == '__main__':
url = "https://movie.douban.com/top250?start=0"
askurl(url)
【相关推荐】
第 3 周的目标是熟悉软件开发的整体过程。你不需要掌握所有的知识,但是你应该知道一些常识,因为它们会影响你的日常工作。
第一天:数据库基础(6 小时):基本 SQL 查询(创建表、选择、Where 查询、更新)、SQL 函数(Avg、Max、Count)、关系数据库(规范化)、内连接、外连接等
第二天:使用 Python 数据库(5 小时):利用一种数据库框架(SQLite 或 panda),连接到一个数据库,在多个表中创建并插入数据,再从表中读取数据。
第三天:API(5 小时):如何调用 API。学习 JSON、微服务(micro-service)以及表现层应用程序转换应用程序接口(Rest API)。
第四天:Numpy(4 小时):熟悉并练习前 30 个 Numpy 习题
第五、六天:作品集网站(一天 5 小时):学习 Django,使用 Django 构建一个作品集网站,也要了解一下 Flask 框架。
第七天:单元测试、日志、调试(5 小时):学习单元测试(PyTest),如何设置和查看日志,以及使用断点调试。
相信这几周下来对于python的学习更有信心了,当然在接下来的学习过程中会更加轻松。此外Python的应用方向很广。大家在Python基础知识学完之后,如果应用方向不同,要学习的东西也会大不同。