import requests
import re
def getHTMLText(url): #请求
try:
r=requests.get(url,timeout=30)
r.raise_for_status()
r.encoding=r.appareent_encoding
return r.text
except:
return ""
print("")
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(plt[i].split(':')[1])
ilt.append([price,title])
except:
print("")
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]))
print("")
def main():
goods="书包" #商品名
depth=2 #爬取多少页面
start_url=’https://s.taobao.com/search?q‘ + goods #连接
infolist=[]
for i in range(depth):
try:
url=start_url+'&s='+str(44*i)
html=getHTMLText(url)
parsePage(infolist,html)
except:
continue
printGoodslist(infolist)
main()
python爬虫代码报错,count=0提示语法错误。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- Nick Peng 2021-04-10 01:02关注
count = 0的上面一行,少了一个右括号,如下:
print(tplt.format({"序号", "价格", "商品名称"})
还有:这一行的单引号也有问题:
start_url =’https: // s.taobao.com / search?q‘ + goods # 连接
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥30 Matlab打开默认名称带有/的光谱数据
- ¥50 easyExcel模板 动态单元格合并列
- ¥15 res.rows如何取值使用
- ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
- ¥15 CSP算法实现EEG特征提取,哪一步错了?
- ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
- ¥15 vue3前端取消收藏的不会引用collectId
- ¥15 delphi7 HMAC_SHA256方式加密
- ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
- ¥15 下列c语言代码为何输出了多余的空格