|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import re
import requests
def getHTMLText(url,header):
try:
r = requests.get(url, headers=header, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
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):
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 = 2
start_url = 'https://s.taobao.com/search?q=' + goods
header = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
"Cookie": 'miid=203625204903725999; enc=F6jHbYWC3qKagm%2BtD1rqmotjewfXBFiarb0xBkHjwY347QXUYsMnqlLLE13bwUQyYu%2Bu0unUHamdz3NJQhnz1zZ%2FeoheN21xeXQjM31onCQ%3D; t=0f2783d5c9a289e9e023609d924f655d; cna=ZtNRGiGAqEwCAXWIP2/Vo/sj; xlly_s=1; sgcookie=E100wO0MnXWkBisYgjz4%2FwE%2F79shtKoPm1nhsmYkjKtgbEI9KwvLfHCKwYocKB6LRvg1mhTmV9p6hKmreOOoUAD0WrSMjiZrcf7zPjJqJU6c9NIS8vTphgn7DZlA9P49s%2BNS; uc3=nk2=F5RFhB0%2FkyOMVBaq&id2=UUphw2eawPynlCSO1Q%3D%3D&lg2=Vq8l%2BKCLz3%2F65A%3D%3D&vt3=F8dCv4UxEWblEVwFcuU%3D; lgc=tb0439756155; uc4=id4=0%40U2grGNhj7ogXjdhiImhjbjQ%2BbXZk4pfC&nk4=0%40FY4O7bKCfmLj%2BRkOGZwTbicJ8EK25lE%3D; tracknick=tb0439756155; _cc_=V32FPkk%2Fhw%3D%3D; mt=ci=-1_0; uc1=cookie14=UoeyChIfBxGNZw%3D%3D; thw=cn; _tb_token_=35968843fe5be; _m_h5_tk=fe7cec091e84ecea1f18b7bfd877ea73_1664022261268; _m_h5_tk_enc=a3229254cec97cc1c99f91975bedd84e; alitrackid=www.taobao.com; lastalitrackid=www.taobao.com; JSESSIONID=8519BC4C36CA35D5AFE8381371B29BD3; isg=BHl5Fx64PkYQOuE6vk7UeLXwiOVThm04mhOXlZuv3aAfIpu049XlCKMwpCbUmgVw; l=eBSie1HRjfmvf4KtBO5ahurza77tPIObzsPzaNbMiInca1u5tFTDdNCEYF6BSdtjgtfbKetPkdi_7REk7izdg2HvCbKrCyClVxJ6-; tfstk=ce7NBdvoVPUai25YeeYV8naJ-2xOaW-D42Rvj8KS89I_GAbkUsAtHB2t6BR834-G.'
}
infoList = []
for i in range(depth):
try:
url = start_url + '&s=' + str(44*i)
html = getHTMLText(url, header)
parsePage(infoList, html)
except:
continue
printGoodsList(infoList)
if __name__ == '__main__':
main()
|
|