woaiyibao
woaiyibao
2019-04-12 11:22
采纳率: 64.3%
浏览 625

爬虫如何用一个循环写出4个变量

https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_id=28286&from_mid=1&&format=json&ie=utf-8&oe=utf-8&query=%E7%94%B5%E5%BD%B1&sort_key=16&sort_type=1&stat0=%E7%88%B1%E6%83%85&stat1=%E5%A4%A7%E9%99%86&stat2=2017&stat3=&pn=0

这里面 stat0 stat1 stat2 都固定有9种左右
pn是以8的倍数递增

urlbase = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_id=28286&from_mid=1&&format=json&ie=utf-8&oe=utf-8&query=%E7%94%B5%E5%BD%B1&sort_key=16&sort_type=1&stat0=&stat1=&stat2=&stat3=&pn="
urllist =[]
for i in range(1,124):
        url = urlbase+str(8*i)
        urllist.append(url)

现在想写一个循环能够把所有种类都放入一个list 不知道怎么办了= =

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • zhengyajun_email
    Yajun-Z 2019-04-12 13:08
    已采纳

    这里你可以用format来实现,在链接中置入占位符{},后面统一赋值:

    urlbase = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_id=28286&from_mid=1&&format=json&ie=utf-8&oe=utf-8&query=%E7%94%B5%E5%BD%B1&sort_key=16&sort_type=1&stat0={0}&stat1={1}&stat2={2}&stat3={3}&pn={4}"
    
    urllist =[]
    for i in range(1,124):
        stat0 = i   # 定义值,我不知道你的值是怎么定义的,用i举例
        stat1 = i
        stat2 = i
        stat3 = i
        pn = 8*i
        url = urlbase.format(stat0, stat1, stat2, stat3, pn)
        urllist.append(url)
    
    点赞 评论

相关推荐