大家好,又见面了,我是你们的朋友全栈君。
# 导入requests和re正则库
import requests
import re
# 定义第一个函数实现获取网页数据
def getHTMLText(url,loginheaders):
try:
r = requests.get(url,headers=loginheaders, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
print(r.text)
return r.text
except:
return ""
#定义一个函数实现把书包信息存储区来,包括编号,价格,名称
def parsePage(ilt, html):
try:
# 要明白 .* 代表的是任意个不同字符,而不是说必须是任意个相同的字符,其他的也是类似
# re.findall()返回的是列表类型
# 利用正则表达式查找价格("view_price":"任意个数的任意数字加点"),所以正则表达式还可在小数点后加两个0写成r'\"view_price\"\:\"[\d\.]*00\"'
plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"', html)
# 利用正则表达式查找题目("raw_title":"任意个数的任意字符")
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列表当中,二维元组列表
ilt.append([price, title])
except:
print("")
# 打印题目和爬取的信息
def printGoodsList(ilt):
# 定义输出格式
tplt = "{:4}\t{:8}\t{:16}"
print(tplt.format("序号", "价格", "商品名称"))
# 循环遍历打印爬取到的信息,第一种方式
for i in range(len(ilt)):
print(tplt.format(i + 1, ilt[i][0], ilt[i][1]))
# 第二种方式,g是从ilt循环遍历每元组的每项,g[0],g[1]是价格和题目
# count = 0
# for g in ilt:
# count = count + 1
# print(tplt.format(count, g[0], g[1]))
#定义主函数,写一下变量
def main():
goods = '书包'
# depth是爬取的深度,也就是爬取几页
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)
# 设置登录的头信息,注意cookie是登陆页面点击登录获取的头信息
loginheaders = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"cookie": "t=4ddf8b70981503ff445f594236c71e96; thw=cn; cookie2=12bdc5dde93e3514edca199a193f232f; _tb_token_=e45e8be4e50be; _samesite_flag_=true; enc=201s0rRJEHeguaLLCC6IAbLJao3k%2FWpbaR4FH6jpx1T6haa1auRivMShxlx1S0Ul3c3meKsTzPUcwTv3aEzt1Q%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; alitrackid=www.taobao.com; lastalitrackid=login.taobao.com; mt=ci=0_0; JSESSIONID=C7EA2D8019D08587402413BDBF38AFF0; cna=k8/5Fvt7vU8CAbfIPj2evd1M; l=eBxklgrHqCn48L6LBOfZourza77TlIRfguPzaNbMiT5P_2fH75cAWZjFnt8MCnGVnsZw-354uljQBrT8xyUBh6Yl3ZQ7XPQo3dTh.; isg=BFFRjiZN687i_QTKjM76nskzYF3rvsUwkMIhzjPmIpg42nAseQpbAHY4fK48Ul1o; tfstk=cwyCBJqLmeYQTTeeQzsZTKVPlrkPZj_sj6gLA5hd3EP90jECibvqlvj4ZFvtMc1..; sgcookie=EjxPeg5aM1t9jg2xwmUNw; unb=2639049752; uc1=cookie14=UoTUMtQjutczRQ%3D%3D&cookie21=VFC%2FuZ9ajC0X15Rzt0LhxQ%3D%3D&pas=0&existShop=false&cookie15=WqG3DMC9VAQiUQ%3D%3D&cookie16=VT5L2FSpNgq6fDudInPRgavC%2BQ%3D%3D; uc3=nk2=2nZbzUmLMMCi2g%3D%3D&vt3=F8dBxGXMemSB7fdhNUQ%3D&lg2=V32FPkk%2Fw0dUvg%3D%3D&id2=UU6idYdXStUjdg%3D%3D; csg=8948e7ae; lgc=%5Cu90ED%5Cu5DDD%5Cu5DDD1998; cookie17=UU6idYdXStUjdg%3D%3D; dnk=%5Cu90ED%5Cu5DDD%5Cu5DDD1998; skt=b778092dd81708b9; existShop=MTU4ODQ3NzIxNQ%3D%3D; uc4=id4=0%40U2xvIZeyY044%2Fg4ssnvrOTWRM69N&nk4=0%402EwyHO%2FQs1K5Yt3PADSqX0DTequA; tracknick=%5Cu90ED%5Cu5DDD%5Cu5DDD1998; _cc_=V32FPkk%2Fhw%3D%3D; _l_g_=Ug%3D%3D; sg=82b; _nk_=%5Cu90ED%5Cu5DDD%5Cu5DDD1998; cookie1=BvGDAyiO3yivOSSiEiVryF%2FdX85RndH78rFBY0y3kOc%3D"
}
html = getHTMLText(url, loginheaders)
parsePage(infoList, html)
except:
continue
printGoodsList(infoList)
main()
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149106.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...