import requests
import re
'''
目标:获取淘宝搜索页面的信息,提取其中的商品名称和价格
理解: 淘宝的搜索接口 翻页的处理
技术路线:requests‐bs4‐re
'''
#步骤1:提交商品搜索请求,循环获取页面
def get_html_text(url):
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
try:
coo = 't=85db5e7cb0133f23f29f98c7d6955615; cna=3uklFEhvXUoCAd9H6ovaVLTG; isg=BM3NGT0Oqmp6Mg4qfcGPnvDY3-pNqzF2joji8w9SGWTYBu241_taTS6UdFrF3Rk0; miid=983575671563913813; thw=cn; um=535523100CBE37C36EEFF761CFAC96BC4CD04CD48E6631C3112393F438E181DF6B34171FDA66B2C2CD43AD3E795C914C34A100CE538767508DAD6914FD9E61CE; _cc_=W5iHLLyFfA%3D%3D; tg=0; enc=oRI1V9aX5p%2BnPbULesXvnR%2BUwIh9CHIuErw0qljnmbKe0Ecu1Gxwa4C4%2FzONeGVH9StU4Isw64KTx9EHQEhI2g%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; mt=ci=0_0; hibext_instdsigdipv2=1; JSESSIONID=EC33B48CDDBA7F11577AA9FEB44F0DF3'
cookies = {}
for line in coo.split(';'): # 浏览器伪装
name, value = line.strip().split('=', 1)
cookies[name] = value
r = requests.get(url, cookies=cookies, headers=headers, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ''
# 步骤2:对于每个页面,提取商品名称和价格信息
def parse_page(ilt, html):
try:
plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"', html) # findall搜索全部字符串,viex_price是源代码中表价格的值,后面的字符串为数字和点组成的字符串
tlt = re.findall(r'\"raw_title\"\:\".*?\"', html) # 找到该字符串和后面符合正则表达式的字符串
for i in range(len(plt)):
price = eval(plt[i].split(':')[1]) # re.split() 将一个字符串按照正则表达式匹配结果进行分割,返回列表类型
title = eval(tlt[i].split(':')[1]) # 将re获得的字符串以:为界限分为两个字符串,并取第二个字符串
ilt.append([price, title])
except:
print('')
# 步骤3:将信息输出到屏幕上
def print_goods_list(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 = 3 # 要爬取几页
start_url = 'https://s.taobao.com/search?q=' + goods
info_list = []
for i in range(depth):
try:
url = start_url + '&s=' + str(44*i) # 44是淘宝每个页面呈现的宝贝数量
html = get_html_text(url)
parse_page(info_list, html)
except:
continue
print_goods_list(info_list)
main()
结果: