python3.x爬虫:按页爬取淘宝商品列表

import requests
import re
'''https://s.taobao.com/search?initiative_id=tbindexz_20170315&ie=utf8&spm=a21bo.50862.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q=%E4%B9%A6%E5%8C%85&suggest=0_1&_input_charset=utf-8&wq=shubao&suggest_query=shubao&source=suggest
https://s.taobao.com/search?initiative_id=tbindexz_20170315&ie=utf8&spm=a21bo.50862.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q=%E4%B9%A6%E5%8C%85&suggest=0_1&_input_charset=utf-8&wq=shubao&suggest_query=shubao&source=suggest&bcoffset=0&ntoffset=0&p4ppushleft=1%2C48&s=44
https://s.taobao.com/search?initiative_id=tbindexz_20170315&ie=utf8&spm=a21bo.50862.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q=%E4%B9%A6%E5%8C%85&suggest=0_1&_input_charset=utf-8&wq=shubao&suggest_query=shubao&source=suggest&bcoffset=-3&ntoffset=-3&p4ppushleft=1%2C48&s=88'''
#获取text
def getHTMLText(url):
	try:
		r = requests.get(url, timeout = 30)
		r.raise_for_status()
		r.encoding= r.apparent_encoding
		return r.text
	except:
		return ""


def paserPage(list,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])
			list.append([price,title])
	except:
		print("出丑") 


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

def main():
	goods = '书包'
	depth = 3 #爬取页数
	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)
			#print(html)
			paserPage(infoList,html)
		except:
			continue
	#print(infoList)
	printGoodsList(infoList)

main()



Python爬虫进行淘宝爬取时,可以通过分析淘宝的分机制来实现。通常,淘宝商品列表会以分的形式展现,每一包含一定数量的商品信息。为了获取多的数据,你需要分析每一请求的URL,确定分参数,然后编写循环代码依次请求每一,提取需要的数据。 一般步骤如下: 1. 分析淘宝的分URL,通常分信息会包含在URL的参数中,比如`page`参数表示当前码。 2. 使用requests库发送HTTP请求,获取网内容。 3. 解析网内容,提取所需信息。常用的解析库有BeautifulSoup或lxml。 4. 判断是否已经爬取到所有面,如果没有,则修改URL参数(比如增加码),继续爬取。 下面是一个简单的代码示例: ```python import requests from bs4 import BeautifulSoup headers = { &#39;User-Agent&#39;: &#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3&#39;} def get_html(url): response = requests.get(url, headers=headers) if response.status_code == 200: return response.text return None def parse_html(html): soup = BeautifulSoup(html, &#39;html.parser&#39;) # 根据实际面结构提取商品信息,这里用find_all举例 items = soup.find_all(&#39;div&#39;, class_=&#39;item_class&#39;) # 假设商品信息都在item_class类的div标签里 for item in items: # 提取商品标题,价格等信息 title = item.find(&#39;a&#39;, class_=&#39;title_class&#39;).get_text() price = item.find(&#39;span&#39;, class_=&#39;price_class&#39;).get_text() print(title, price) def main(): base_url = &#39;淘宝商品列表基础URL&#39; page = 1 pages_count = 10 # 假设你想爬取10数据 while page <= pages_count: url = base_url + f&#39;?page={page}&#39; html = get_html(url) if html: parse_html(html) else: print(&#39;获取面失败&#39;) page += 1 if __name__ == &#39;__main__&#39;: main() ``` 需要注意的是,爬取淘宝或任何其他网站时,应遵守网站的Robots协议和版权法规定,避免爬取行为对网站造成过大压力或违反法律法规。此外,淘宝等电商平台通常有反爬虫机制,可能需要处理JavaScript渲染的面、动态加载的数据、登录验证、Ajax请求等复杂情况。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值