首页 新闻 会员 周边 捐助

python 报错 AttributeError: 'module' object has no attribute 'urllopen' 问题

0
悬赏园豆:10 [待解决问题]

python小白 调试程序 遇到报错

 

from urllib import request, parse


这句话报错

AttributeError: 'module' object has no attribute 'urllopen'
elivs的主页 elivs | 初学一级 | 园豆:176
提问于:2017-07-31 16:12
< >
分享
所有回答(2)
0

参考http://blog.csdn.net/aug0st/article/details/42707709

ycyzharry | 园豆:25683 (高人七级) | 2017-07-31 16:16

好的 我看看 谢谢

支持(0) 反对(0) elivs | 园豆:176 (初学一级) | 2017-07-31 16:40
0

这个跟from urllib import request, parse没有关系,是用到urllopen的那行代码有错,多粘贴一点代码出来

悟行 | 园豆:12559 (专家六级) | 2017-07-31 16:20

def SearchPageCount(position, city):
url = 'http://www.lagou.com/jobs/positionAjax.json?'
params = {'city': city, 'kd': position}
url += parse.urlencode(params)
with request.urlopen(url) as f:
data = f.read()
content = json.loads(str(data, encoding='utf-8', errors='ignore'))['content']
count = int(content['totalPageCount'])
totalCount = int(content['totalCount'])
print('本次搜索到'.format(totalCount))
return count

def get_rdata(url):
data = request.urlopen(url).read()
params = parse.parse_qs(parse.urlparse(url).query)
print('正在解析第{0}页...'.format(params.get('pn', [''])[0]))
# 读取Json数据
jsondata = json.loads(str(data, encoding='utf-8', errors='ignore'))['content']['result']
for t in list(range(len(jsondata))):
jsondata[t]['companyLabelListTotal'] = '-'.join(jsondata[t]['companyLabelList'])
jsondata[t].pop('companyLabelList')

支持(0) 反对(0) elivs | 园豆:176 (初学一级) | 2017-07-31 16:39

 这个么

支持(0) 反对(0) elivs | 园豆:176 (初学一级) | 2017-07-31 16:40

@elivs: 

from urllib import request, parse


def SearchPageCount(position, city):
    url = 'http://www.lagou.com/jobs/positionAjax.json?'
    params = {'city': city, 'kd': position}
    url += parse.urlencode(params)
    with request.urlopen(url) as f:
        data = f.read()
        content = json.loads(str(data, encoding='utf-8', errors='ignore'))['content']
        count = int(content['totalPageCount'])
        totalCount = int(content['totalCount'])
        print('本次搜索到{0}'.format(totalCount))
        return count


def get_rdata(url):
    data = request.urlopen(url).read()
    params = parse.parse_qs(parse.urlparse(url).query)
    print('正在解析第{0}页...'.format(params.get('pn', [''])[0]))
    # 读取Json数据
    jsondata = json.loads(str(data, encoding='utf-8', errors='ignore'))['content']['result']
    for t in list(range(len(jsondata))):
        jsondata[t]['companyLabelListTotal'] = '-'.join(
            jsondata[t]['companyLabelList'])
        jsondata[t].pop('companyLabelList')

执行代码没有报错AttributeError: 'module' object has no attribute 'urllopen'

支持(0) 反对(0) 悟行 | 园豆:12559 (专家六级) | 2017-08-01 09:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册