| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
大量收QQ微信精准粉/交友粉,非诚勿扰千里之行 始于足下
共有 326 人关注过本帖
标题:这个“for”有问题吗???
只看楼主 加入收藏
周煜
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2018-7-12
结帖率:100%
  问题点数:0  回复次数:1   
这个“for”有问题吗???
倒数第五行
程序代码:

import requests as r
import re
import time as t

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/\
    65.0.3325.181 Safari/537.36
'
}
client = pmg.MongoClient('localhost', 27017)
mydb = client['mydb']
d = mydb['doupochangqiong']
a = []

try:
    def get_info(url):
        global a
        res = r.get(url, headers=headers)
        if res.status_code == 200:
            contents = re.findall('<p>(.*?)</p>', res.content.decode('utf-8'), re.S)
            for content in contents:
                content = re.sub(' www.doupoxs.com ', '', content)
                content = re.sub('&\\w;','"',content)
                a.append(content)
        else:
            pass

    if __name__ == '__main__':
        urls = ['http://www.doupoxs.com/doupocangqiong/{}.html'.format(str(i)) for i in range(1, 1665)]
        for urla in urls:
            get_info(url=urla)
            t.sleep(1)
        b = ''
        b += a[c-1: c]for c in range(1, len(a) + 1)
        info = {'dou': b}
        d.insert_one(info)
except:
    pass



[此贴子已经被作者于2018-7-12 17:51编辑过]

2018-07-12 17:38
zjd873721519
Rank: 1
等 级:新手上路
威 望:1
帖 子:6
专家分:7
注 册:2017-8-15
  得分:0 
原文代码:
urls = ['http://www.doupoxs.com/doupocangqiong/{}.html'.format(str(i)) for i in range(1, 1665)]
修改为:
urls = ['http://www.doupoxs.com/doupocangqiong/{0}.html'.format(str(i)) for i in range(1, 1665)]
否则urls获取不到需要的值

参考文章:
https://blog.csdn.net/zjd873721519/article/details/80879925
2018-07-18 10:52







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027111 second(s), 9 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved