标签:keyword 爬虫 response 案例 ilt html 淘宝 page def
import requests
import re
def getHtml(keyword, page=1):
payload = {'q': keyword,
's': str((page-1)*44)}
headers = {'authority': 's.taobao.com',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36',
'cookie': 'Your cookie'}
url = 'https://s.taobao.com/search'
response = requests.get(url, params=payload, headers= headers, timeout=30)
response.raise_for_status()
response.encoding = response.apparent_encoding
return response.text
# def open_url(keyword, page=1):
# keyload = {'q': keyword, 's': str((page - 1) * 44), 'sort': 'sale-desc'}
def parserHtml(ilt, html):
try:
# item = re.search(r'g_page_config = (.*);\n', html) # 爬取整个商品页面
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])
return ilt
except:
print("解析错误")
def printList(ilt):
try:
tplt = "{:4}\t{:8}\t{:16}"
print(tplt.format("序号", "价格", "名称"))
count = 0
for i in ilt:
count += 1
print(tplt.format(count, i[0], i[1]))
except:
print("打印出现错误")
def main():
ilt = []
keyword = input("你想搜索的商品")
html = getHtml(keyword, page=2)
ilt = parserHtml(ilt, html)
printList(ilt)
main()
标签:keyword,爬虫,response,案例,ilt,html,淘宝,page,def 来源: https://www.cnblogs.com/RankFan/p/15254432.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。