#coding:utf8 #使用Requests访问http://seputu.com/,获取并打印HTML文档内容 import requests user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent':user_agent} r = requests.get('http://seputu.com/', headers=headers) print r.text #分析网页的HTML结构: #标题和章节都被包含在<div class="mulu">标记下,标记位于其中的<div class="mulu-title">下的<h2>中, #章节位于其中的<div class="box">下的<a>中 from bs4 import BeautifulSoup soup = BeautifulSoup(r.text,'html.parser',from_encoding='utf-8') for mulu in soup.find_all(class_="mulu"): h2 = mulu.find('h2') if h2 != None: h2_title = h2.string #获取标题 for a in mulu.find(
数据存储(无数据库版)之一:存储为JSON
最新推荐文章于 2024-04-12 11:12:54 发布