python爬虫爬取淘宝网页

首先进行相关的分析

要想爬取相关的信息,必须指导如下信息:

1、访问接口

2、翻页操作

首先进行搜索,得到相关的网址:https://s.taobao.com/search?q=书包&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.50862.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170501

然后进行查看第二页的网址:https://s.taobao.com/search?q=书包&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.50862.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170501&bcoffset=4&ntoffset=4&p4ppushleft=1%2C48&s=44

继续查看第三页的网址:https://s.taobao.com/search?q=书包&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.50862.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170501&bcoffset=4&ntoffset=4&p4ppushleft=1%2C48&s=88

进行仔细的观察就知道其中的奥妙所在

所以我们整个程序的设计结构如下:

1、提交商品搜索请求,循环获取页面

2、对于每个页面,提取商品名称和价格信息

3、将信息输出到屏幕上

主要的函数有

1、爬取网页

def getHTMLText(url):
	try:
		r=requests.get(url,timeout=30)
		r.raise_for_status()
		r.encoding=r.apparent_encoding
		return r.text
	except:
		print("")


2、进行信息提取

def parsePage(ilt,html):
	try:
		#在爬取下来的网页中进行查找价格
		plt=re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
		#在爬取下来的网页中查找物品
		tlt=re.findall(r'\"raw_title\"\:\".*?\"',html)
		for i in range(len(plt)):
			price=eval(plt[i].split(':')[1])
			title=eval(tlt[i].split(':')[1])
			ilt.append([price,title])
	except:
		print("")


3、进行输出

def printGoodsList(ilt):
	tplt = "{:4}\t{:8}\t{:16}"
	print(tplt.format("序号", "价格", "商品名称"))
	count = 0
	for g in ilt:
		count = count + 1
		print(tplt.format(count, g[0], g[1]))


4、主函数

def main():
	goods='书包'
	depth=2
	start_url='https://s.taobao.com/search?q='+goods
	infoList=[]
	for i in range(depth):
		try:
			#str函数的作用是将其中的内容转换为字符串
			url=start_url+'&s='+str(44*i)
			html=getHTMLText(url)
			parsePage(infoList , html)
		except:
			continue
	printGoodsList(infoList)

下面贴出完整的代码

import requests
import re

#获取页面函数、
def getHTMLText(url):
	try:
		r=requests.get(url,timeout=30)
		r.raise_for_status()
		r.encoding=r.apparent_encoding
		return r.text
	except:
		print("")
#对获取页面进行解析
def parsePage(ilt,html):
	try:
		#在爬取下来的网页中进行查找价格
		plt=re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
		#在爬取下来的网页中查找物品
		tlt=re.findall(r'\"raw_title\"\:\".*?\"',html)
		for i in range(len(plt)):
			price=eval(plt[i].split(':')[1])
			title=eval(tlt[i].split(':')[1])
			ilt.append([price,title])
	except:
		print("")
#进行打印
def printGoodsList(ilt):
	tplt = "{:4}\t{:8}\t{:16}"
	print(tplt.format("序号", "价格", "商品名称"))
	count = 0
	for g in ilt:
		count = count + 1
		print(tplt.format(count, g[0], g[1]))

def main():
	goods='书包'
	depth=2
	start_url='https://s.taobao.com/search?q='+goods
	infoList=[]
	for i in range(depth):
		try:
			#str函数的作用是将其中的内容转换为字符串
			url=start_url+'&s='+str(44*i)
			html=getHTMLText(url)
			parsePage(infoList , html)
		except:
			continue
	printGoodsList(infoList)

main()


好的,下面是Python爬取淘宝商品信息的基本思路: 1. 分析淘宝商品搜索页面的HTML结构,确定需要爬取的数据,如商品标题、价格、销量、评论数、店铺名称等。 2. 使用Python爬虫框架(如requests、Scrapy等)向淘宝商品搜索页面发送请求,获取页面HTML源代码。 3. 使用Python的HTML解析库(如BeautifulSoup、pyquery等)解析HTML源代码,获取需要的数据。 4. 将获取到的数据保存到本地文件或数据库中。 下面是一个简单的Python爬取淘宝商品信息的示例代码: ```python import requests from bs4 import BeautifulSoup def get_taobao_info(keyword): url = 'https://s.taobao.com/search?q={}'.format(keyword) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'lxml') items = soup.select('.item.J_MouserOnverReq') for item in items: title = item.select('.title')[0].get_text().strip() price = item.select('.price')[0].get_text() sale = item.select('.deal-cnt')[0].get_text() shop = item.select('.shop')[0].get_text().strip() print('商品:{},价格:{},销量:{},店铺:{}'.format(title, price, sale, shop)) if __name__ == '__main__': keyword = '手机' get_taobao_info(keyword) ``` 这段代码实现了对淘宝商品搜索页面的爬取,并输出了商品的标题、价格、销量和店铺名称。你可以根据需要修改代码,获取更多商品信息。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值