使用Beautifulsoup爬取药智网数据
使用Beautifulsoup模块爬取药智网数据
Tips:1.爬取多页时,先用一页的做测试,要不然ip容易被封
2.自己常用的处理数据的方法:
reg=re.compile('正则表达式')
data=reg.sub('要替换的字符串',data)
代码(其实没多少)
# encoding=utf-8
from bs4 import BeautifulSoup
import urllib2
import time
class YBZC():
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/zhuce?p='+str(pageIndex)
request=urllib2.Request(url,headers=self.headers)
respone=urllib2.urlopen(request)
html=respone.read()
return html
except urllib2.URLError,e:
if hasattr(e,'reason'):
print u"连接失败",e.reason
return None
def getItems(self):
for i in range(1,13):
html=self.getHtml()
soup=BeautifulSoup(html,"html.parser")
tr_list=soup.find_all('tr')
# 表格标题
if i==1:
for item in tr_list[0]:
if item not in ['\n','\t',' ']:
with open('yaopinzhuce1030.txt','a') as f:
f.write(item.get_text(strip=True).encode('utf-8')+'|')
#=========================2015-10-30================================
# 第一次的时候是现将数据全部都取下来,等存入文件的时候再筛选,现在直接筛选再
# 存入文件中,当时的时候并没有想到并且没有理解get_text()方法,这个也是
# 代码不精简的原因。。。。
#===================================================================
# list_tit=[]
# for ths in tr_list[0]:
# if ths.find('a'):
# for item in ths:
# if type(item)!=unicode:
# list_tit.append(item.string)
# else:
# list_tit.append(ths.get_text(strip=True))
# for item in list_tit:
# if item not in ['',' ','\n','\t']:
# with open('yaopinzhuce_new.txt','a') as f:
# f.write(item.encode('utf-8')+'|')
# 表格内容
f=open('yaopinzhuce1030.txt','a')
for tr in tr_list[1:]:
f.write('\n')
for item in tr:
if item not in ['',' ','\n']:
if item.string==None:
f.write('None'+'|')
else:
f.write(item.string.encode('utf-8')+'|') f.close()
print 'sleeping... pageloading %d/12' %i
time.sleep(5) spider=YBZC()
spider.getItems()
使用Beautifulsoup爬取药智网数据的更多相关文章
- 八爪鱼采集器︱爬取外网数据(twitter、facebook)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 要想采集海外数据有两种方式:云采集+单机采集. ...
- python3 requests + BeautifulSoup 爬取阳光网投诉贴详情实例代码
用到了requests.BeautifulSoup.urllib等,具体代码如下. # -*- coding: utf-8 -*- """ Created on Sat ...
- java爬虫,爬取当当网数据
背景:女票快毕业了(没错!我是有女票的!!!),写论文,主题是儿童性教育,查看儿童性教育绘本数据死活找不到,没办法,就去当当网查询下数据,但是数据怎么弄下来呢,首先想到用Python,但是不会!!百 ...
- python爬虫爬取赶集网数据
一.创建项目 scrapy startproject putu 二.创建spider文件 scrapy genspider patubole patubole.com 三.利用chrome浏览器 ...
- 【转】java爬虫,爬取当当网数据
背景:女票快毕业了(没错!我是有女票的!!!),写论文,主题是儿童性教育,查看儿童性教育绘本数据死活找不到,没办法,就去当当网查询下数据,但是数据怎么弄下来呢,首先想到用Python,但是不会!!百 ...
- python爬取返利网中值得买中的数据
先使用以前的方法将返利网的数据爬取下来,scrapy框架还不熟练,明日再战scrapy 查找目标数据使用的是beautifulsoup模块. 1.观察网页,寻找规律 打开值得买这块内容 1>分析 ...
- 初识python 之 爬虫:爬取中国天气网数据
用到模块: 获取网页并解析:import requests,html5lib from bs4 import BeautifulSoup 使用pyecharts的Bar可视化工具"绘制图表& ...
- 使用python爬取东方财富网机构调研数据
最近有一个需求,需要爬取东方财富网的机构调研数据.数据所在的网页地址为: 机构调研 网页如下所示: 可见数据共有8464页,此处不能直接使用scrapy爬虫进行爬取,因为点击下一页时,浏览器只是发起了 ...
- 基于爬取百合网的数据,用matplotlib生成图表
爬取百合网的数据链接:http://www.cnblogs.com/YuWeiXiF/p/8439552.html 总共爬了22779条数据.第一次接触matplotlib库,以下代码参考了matpl ...
随机推荐
- 6.Git内容修改之后的查看和提交
我们已经成功地添加并提交了一个readme.txt文件,现在,是时候继续工作了,于是,我们继续修改readme.txt文件,改成如下内容: Git is a distributed version c ...
- Lucene.net
模糊查询-〉数据库全文检索-〉Lucene 一元分词(lucene内置) Analyzer analyzer = new CJKAnalyzer(); TokenStream tokenStream ...
- gerrit add review标签
添加verifyed标签 http://blog.csdn.net/terence427/article/details/16840697
- LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16 Debug/firstapi.exe :
#include <windows.h> #include <iostream.h> void main() { char SourceFileName[MAX_PATH]; ...
- 1,Boost -> Bind
#include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...
- Wpf 中的DataGrid的Header属性,动态bind时不起作用
在使用wpf开发软件时,有使用到DataGrid,DataGridTextColumn的Header 属性使用DynamicResource binding,在修改绑定数据源时,header并没有更新 ...
- C语言-循环结构及break、continue
循环结构 --1-- 结构循环 1.1 while循环 1.2 do…while循环 1.3 for循环 --2-- break和continue 2.1 break关键字 2.2 continue关 ...
- 简述id,instancetype和__kindof的区别
id: 好处:可以调用任何对象方法 坏处:不能进行编译检查 + (id)person; instancetype 好处:自动识别当前类的对象 坏处:不会提示返回的类型 + (instancetype) ...
- jquery on和bind
1:bind():为每个匹配元素的特定事件绑定事件处理函数. bind(type,[data],fn) type: 含有一个或多个事件类型的字符串,由空格分隔多个事件. 比如"click&q ...
- 关于IE8
IE8对H5,CSS3,还有脚本的兼容都存在各种问题,调padding的时候老是忘记IE盒子跟W3C盒子模型不一样: IE8兼容CSS3透明度的方法,采用滤镜实现: (1)filter ...