使用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 ...
随机推荐
- JS倒计时功能,给定时间返回规定格式倒计时时间
重写了一下,增强了通用性,重写时发现月份和年份计算很复杂,因为月份天数不一样,年份又涉及平年闰年; 本人数学实在不佳无法写出算法,哪位大大日后写出来了分享给小弟一份,谢谢~. <!doctype ...
- Solr5 DataImport 处理1对多关系
“问题” 和“派发处理” 是2个对象. 每个问题可以被派发N次, 最后结束问题. 实际查询中数据量可能很大,同时即可以从问题查派发部门, 也可以从派发情况查询问题(如本部门处理的问题) 使用的技巧就是 ...
- jeesz源码下载
大型分布式企业架构 jeesz,百度去搜索jeesz
- 161229、SpringMVC的各种参数绑定方式
1. 基本数据类型(以int为例,其他类似): Controller代码: @RequestMapping("saysth.do") public void test(int co ...
- Raytracer
http://www.cnblogs.com/miloyip/archive/2010/03/29/1698953.html http://www.scratchapixel.com/lessons/ ...
- UUID工具
package com.tech.jin.util; import java.util.UUID; public class UuidUtil { public static String get32 ...
- JAVA判断当前日期是节假日还是工作日
package com.buybal.bat.util; import java.io.File; import java.io.FileInputStream; import java.io.IOE ...
- POSTMAN as debugger for integration APPs
Chrome Menu: Window > Extensions > Postman - REST Client 0.8.4.10 起个标题,有空总结一下一个经验,关于Netsuite i ...
- 【前端】iterable类型的 forEach方法
看三个例子: a = [ age: 1 , age: 2 ] a.forEach (item) -> item.age = 0 # 有效 console.log a # [ # age: 0 # ...
- java递归应用
/** * 分配人员数据 * @param num 数据条数 * @param taskUs 人员数据 * @param isend 标记第一次循环 */private void distTaskNu ...