使用beautifulsoup写的第一个小爬虫程序

昨天才刚学的这个爬虫的第三方库,被它的强大所震撼,以下为自己写的爬虫的程序:
 

# coding=utf-8
import urllib2
from bs4 import BeautifulSoup
import re

url = "http://tools.2345.com/m/naonao/"
response = urllib2.urlopen(url)
soup = BeautifulSoup(response)
temp = soup.find_all(name="a",attrs={"class":"box"})
print len(temp)

 
for i in range(1,len(temp)):
    temp_key = []
    temp_value = []
    sub_url = url+ temp[i].get('href')
    print sub_url
    sub_response = urllib2.urlopen(sub_url)
    sub_soup = BeautifulSoup(sub_response)
    name = (sub_soup.find(name="span",attrs = {"class":"name"})).string
    date = (sub_soup.find(name="span",attrs ={"class":"date"})).string
    theHead = name+"("+date+")"#theHead是说明星座运势及日期的
    print theHead
    temp1 = sub_soup.find(name = "div",attrs = {"class":"brief"})
    print temp1.text  #temp1为运势主体内容
    temp2 = (sub_soup.find(name="div",attrs = {"class":"taglist"})).text #temp2为其运势中需注意的
    print temp2

运行时会出现这个错误:WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.

使用网上的引入sys模块并重写编码的那个方法并不可行,引入并重写后,发现程序运行后根本没有内容被读写出来,希望哪位看到的朋友若知道如何解决能给以提示下,谢谢

后续~~~~

解决方法已找到,就是要在

sub_soup = BeautifulSoup(sub_response)

这一句中,加入from_encoding,具体为sub_soup = BeautifulSoup(sub_response,from_encoding="GBK")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值