实例一:淘宝商品比价定向爬虫
打开淘宝,输入衬衫,链接为:
第二页的链接为:
第三页的链接为:
发现翻页操作是通过后边的参数s来操作的,每页44个商品。
ps:不知道为什么原来的链接里边 search?q=衬衫 复制过来就变成了上述打不开的链接。
哈哈,这里突然发现 https://s.taobao.com/search?q=衬衫(自己码的,不是复制过来的)就可以进去了。
输入https://s.taobao.com/search?q=衬衫&s=44 哈哈发现翻了页了。
这里提取出名称,价格还有付款人数。
首先分析定向爬虫的可行性
进入网址:http://s.taobao.com/robots.txt 查看
发现禁止爬取,但是可以以类人类行为进行爬取。作为教学实例。
程序结构设计
进入衬衫页面,查看网页源代码,按照第一个衬衫的名称,价格,已购人数搜索源码(ctrl+f)查看数据是怎么存在的。
主程序:
import requests
import re
def getHTMLText(url):
print("")
def parserPage(ilt,html):
print("")
def printGoodsList(ilt):
print("")
def main():
goods="衬衫" #指定商品名称
depth=2 #指定爬取的页面个数
start_url='https://s.taobao.com/search?q='+goods
infoList=[]
for i in range(depth):
try:
url=start_url+"&s="+str(44*i)
html=getHTMLText(url)
parserPage(infoList,html)
except:
continue
printGoodsList(infoList)
main()
对各函数进行填充:
def getHTMLText(url):
try:
r=requests.get(url,timeout=30) #时间限制为30秒
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
return ""
def parserPage(ilt,html):
try:
#返回商品名称的列表,?是最小匹配。加\是为了转义。s
ns=re.findall(r'\"raw_title\"\:\".*?\"',html)
#返回价格的列表,[\d\.]*这个好好消化
jiage=re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
#返回已购人数列表
renshu=re.findall(r'\"view_sales\"\:\".*?\"',html)
for i in range(len(ns)):
#将raw_title去掉,eval函数的意思是吧返回的字符串中的最外层单引号或双引号去掉。
name=eval(ns[i].split(':')[1])
print(name)
price=eval(jiage[i].split(':')[1])
print(price)
people=eval(renshu[i].split(':')[1])
print(people)
ilt.append([price,people,name])
except:
print("")
def printGoodsList(ilt):
tplt="{:<4}\t{:<6}\t{:<10}\t{:<10}"
print(tplt.format("序号","价格","已购人数","商品名称"))
count=0
for j in ilt:
count+=1
print(tplt.format(count,j[0],j[1].split('人')[0],j[2]))
总代码为:
import requests
import re
def getHTMLText(url):
try:
r=requests.get(url,timeout=30) #时间限制为30秒
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
return ""
def parserPage(ilt,html):
try:
#返回商品名称的列表,?是最小匹配。加\是为了转义。s
ns=re.findall(r'\"raw_title\"\:\".*?\"',html)
#返回价格的列表,[\d\.]*这个好好消化
jiage=re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
#返回已购人数列表
renshu=re.findall(r'\"view_sales\"\:\".*?\"',html)
for i in range(len(ns)):
#将raw_title去掉,eval函数的意思是吧返回的字符串中的最外层单引号或双引号去掉。
name=eval(ns[i].split(':')[1])
print(name)
price=eval(jiage[i].split(':')[1])
print(price)
people=eval(renshu[i].split(':')[1])
print(people)
ilt.append([price,people,name])
except:
print("")
def printGoodsList(ilt):
tplt="{:<4}\t{:<6}\t{:<10}\t{:<10}"
print(tplt.format("序号","价格","已购人数","商品名称"))
count=0
for j in ilt:
count+=1
print(tplt.format(count,j[0],j[1].split('人')[0],j[2]))
def main():
goods="衬衫" #指定商品名称
depth=2 #指定爬取的页面个数
start_url='https://s.taobao.com/search?q='+goods
infoList=[]
for i in range(depth):
try:
url=start_url+"&s="+str(44*i)
html=getHTMLText(url)
parserPage(infoList,html)
except:
continue
printGoodsList(infoList)
main()
结果如下:
ps: 注意eval函数这个知识点
实例二:股票数据定向爬虫
对于新浪股票,进入网站后,选取任意一个股票的价格,在网页源码中搜索价格,找不到,说明是由js代码生成。
对于百度股票,进入网站,打开个股,查看其价格是否在HTML页面中。,找到了。说明百度股票适合作为定向爬取的
数据来源。
需要找到包含所有股票的列表,但在百度股票中很难找到一个页面包含所有股票。可以去东方财富网爬取股票列表信息。
http://quote.eastmoney.com/stocklist.html
百度股票中每一个个股网址中都包含了个股的数字代码和2个字母的字符串。
因此程序结构框架为:
主函数