为什么最后打印不出任何东西?

新手上路,请多包涵

为什么最后print是个空的列表?

import re
import requests
url = 'https://s.taobao.com/search?q...'

r = requests.get(url)
r.raise_for_status()
r.encoding = r.apparent_encoding

plt = re.findall(r'"view_price":"[d.]*"',url)
print(plt)

图片描述

阅读 1.9k
1 个回答

根据你的代码,你是想根据关键词抓取某宝部分商品的价格吧?
这里存在两个问题:

  1. 接口问题,我看了下,商品列表是通过JSONP拉取的因此,直接用requests模拟搜索并无法获取到搜搜商品列表,你需要换成https://s.taobao.com/api?callback=jsonp253&ajax=true&m=customized&q=%E4%B9%A6%E5%8C%85
  2. 根据正则匹配结果 re.findall(r'"view_price":"[d.]*"',url)有两个错误,
    a: 匹配结果,但是你给的是url,无法达到预期,换成r.text.encode('UTF8')
    b: 正则错误,浮点数匹配正确的应该为[\d.]*(严谨一点的可以用[1-9]\d*\.\d*|0\.\d*[1-9]\d*
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进