标题:这个“for”有问题吗???
只看楼主
周煜
Rank: 1
等 级:新手上路
帖 子:7
专家分: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. ', '', content)
                content = re.sub('&\\w;','"',content)
                a.append(content)
        else:
            pass

    if __name__ == '__main__':
        urls = ['http://www.{}.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编辑过]

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

参考文章:
https://blog.
2018-07-18 10:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-488543-1-1.html




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

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