python学习问题记录

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 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值