Python爬取药智网的中药材图谱网页

这次学习了python中的BeautifulSoup模块,并用bs爬取的药智网。网址:http://db.yaozh.com/tupu?p=

首先要安装bs,要注意版本问题,我一开始用的beautifulsoup4-4.4.1版本装不上,换成beautifulsoup4-4.2.0就可以了。

这是我写的爬取药智网的代码:

#coding=utf-8
from bs4 import BeautifulSoup
import urllib2
class ZYC:
    def __init__(self):
        self.user_agent = 'Mozilla/4.0(compatible;MSIE 5.5;Windows NT)'
        self.headers = {'User_Agent' :self.user_agent}
    #获取源代码
    def getHtml(self,pageIndex):
        try:
            url='http://db.yaozh.com/tupu?p='+str(pageIndex)
            request=urllib2.Request(url,headers=self.headers)
            response=urllib2.urlopen(request)
            html=response.read()
            return html
        except  urllib2.URLError,e:
            if hasattr(e,'reason'):
                print u'loading error',e.reason
                return None
    #获取每页的数据
    def getPage(self):
        #事先将标题写入
        f=open('zyc.txt','a+')
        f.write("序号|中药材名称|图谱来源|页码|查看图谱|"+"\n")
        for i in range(1,11):
            html=self.getHtml(i)
            soup=BeautifulSoup(html)
            SJ=soup.find_all('tr')
            #删除每页重复类别标题
            #len(SJ)
            SJ.remove(SJ[0])#操作之前要先用len()函数看一下有没有超出列表索引范围
            for item in SJ:
                f.write(item.get_text('|',strip=True).encode('utf-8')+'\n')
        f.close()
spider=ZYC()
f=spider.getPage()
用了bs中的find_all()和get_text()

下面这个是借鉴(链接)的:

 def getSJ(self):
        for s in range(1,11):
            html=self.getHtml(s)
            soup=BeautifulSoup(html)
            pageSJ=soup.find_all('tr')
            if s==1:
                for item in pageSJ[0]:
                    if item not in ['\n','\t',' ']:
                        with open('SJ.txt','a') as f:
                            f.write(item.get_text(strip=True).encode('utf-8')+'|')
            f=open('SJ.txt','a')
            for i in pageSJ[1:]:
                f.write('\n')
                for item in i:
                    if item not in ['\n','\t',' ']:
                        # if item==None:#将空白项填入“None”
                        #     f.write('None'+'|')
                        # else:
                        f.write(item.get_text(strip=True).encode('utf-8')+'|')
            f.close()
因为我所爬取的这个网页没有空白项,所以我的代码可用,但要是有空白项,还是第二个比较好。一直在尝试解决这个问题。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值