您的位置:首页 > 编程语言 > Python开发

python学习问题记录

2017-08-16 16:13 375 查看

1、from bs4 import BeautifulSoup 报错 ImportError: No module named bs4

引用之前我已经安装了 BeautifulSoup 模块,但还是报错,最终原因是我没有安装BeautifulSoup4,使用命令 pip install BeautifulSoup4,之后就解决了

2、从网页抓取的内容不全(还以为网站做了什么限制,不让抓取,因为有的网址使用该方法就挺全的)

url="http://hy.werer.cn/index.php?qb=p&page=2"
request = urllib2.Request(url=url,headers=headers)
response = urllib2.urlopen(request)
html = response.read()
html = BeautifulSoup(html)
解决办法:

修改 BeautifulSoup的参数

html = BeautifulSoup(html,"html5lib",from_encoding='gb2312')
注意这里的html5lib是个模块,要进行安装和引用的 安装使用 pip install html5lib,然后在头部 import html5lib


3、Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock

因为我的mysql.sock在 tmp文件夹下,所以链接项需要设置
db = MySQLdb.connect(host='localhost', user='root', passwd='root', db='python', port=3306, charset='utf8', unix_socket='/tmp/mysql.sock',cursorclass = MySQLdb.cursors.DictCursor)

4、只要脚本里面有中文,运行就报错 but no encoding declared 

在py文件的第一行加上 #coding=utf-8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: