django实战篇二

来源:互联网 发布:linux公社 编辑:程序博客网 时间:2024/06/05 14:53
今天在django的框架下重新运用可beautifulsoup,总结了一点语法
实例一个beautifulsoup对象
soup = bs4.BeautifulSoup(html)
它的字标签一样具有相关方法
我最多用的就是find_all(),它返回的是一个list,list中每个元素都具有相关特性
find_all('ul',attrs={'id':'business_listbox'}),find_all('strong', class_='productntitle')
find_all('a',href=re.compile("search.php\?CataID=&act=txt&tp=store&skey="))
获取某个标签的属性 a['href']
然后写成一个数组用list.append


最后写成字典格式,并以json返回
 new_dict=dict.fromkeys(('name','href'))
    new_dict['name']=all_name
    new_dict['href']=href
    back=json.dumps(new_dict,ensure_ascii=False)
    return HttpResponse(back)


"My name is %s and weight is %d kg!" % ('Zara', 21) 
原创粉丝点击