输入关键字 抓取所有网页(STM32获取TB网页的一些信息(一) )

优采云 发布时间: 2021-10-25 08:01

  输入关键字 抓取所有网页(STM32获取TB网页的一些信息(一)

)

  一、项目介绍

  在 TB 网页上获取一些信息(仅用于教育目的)

  比如我们需要使用关键字来获取TB接口的一些信息。

  通过确认,可以发现请求是:

  https://s.taobao.com/search?q=书包&s=0 #q代表关键字,显示第一页

https://s.taobao.com/search?q=书包&s=44 #显示第二页,每一个44个

  结构设计:

  二、获取分析

  使用的解析方法很多,一种使用BeatifulSoup库,另一种使用正则表达式直接匹配。我们在这里使用正则表达式。

  通过查看源代码,我们可以看到 view_price 和 raw_title 标签正是我们所需要的。

  三、源代码

  # 已失效,需要登录

import requests

import re

def getHTMLText(url):

try:

r = requests.get(url, timeout = 30)

r.raise_for_status()

r.encoding = r.apparent_encoding #防止中文乱码

print(r.text)

return r.text

except:

return ""

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):

print(ilt)

tplt = "{:4}\t{:8}\t{:16}"

print(tplt.format("序号", "价格", "商品名称"))

count = 0

for g in ilt:

count = coount +1

print(tplt.format(count, g[0], g[1]))

def main():

goods = '书包'

depth = 1 #搜索两页,每页44个商品

start_url = 'https://s.taobao.com/search?q=' + goods

infoList = []

for i in range(depth):

try:

url = start_url + '&s=' + str(44*i)

print(url)

html = getHTMLText(url)

parsePage(infoList, html)

except:

continue

printGoodsList(infoList)

main()

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线