Python学习---day14

仿百度页面

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>百度一下,你就知道</title>
		<!-- 引入外部样式 -->
		<link rel="stylesheet" type="text/css" href="./css/style.css">
		<link rel="icon" type="image/jpg" href="img/1.jpg">
	</head>
	<body>
		<!-- top -->
		<div class="top">
			<!-- top-left -->
			<div class="top_left">
				<a href="">新闻</a>
				<a href="">hao123</a>
				<a href="">地图</a>
				<a href="">贴吧</a>
				<a href="">视频</a>
				<a href="">图片</a>
				<a href="">网盘</a>
				<a href="">更多</a>
			</div>
			<!-- top-right -->
			<div class="top_right">
				<a href="" class="set">设置</a>
				<input type="submit" value="登录" class="login">
			</div>
		</div>


		<!-- center -->
		<div class="center">
			<!-- logo -->
			<div>
				<img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" alt="" width="200px"
					height="100px">
			</div>

			<!-- input -->
			<div>
				<input type="text" class="input_text">
				<input type="submit" value="百度一下" class="button">
			</div>

			<!-- table -->
			<div>
				<table>
					<tr>
						<td><b>百度热搜</b></td>
						<td style="text-align: right;">换一换</td>
					</tr>
					<tr>
						<td>
							<span>0</span>
							<span>多措并举助企稳就业</span>
						</td>
						<td>
							<span>3</span>
							<span>山东舰霸气壁纸</span>
						</td>
					</tr>
					<tr>
						<td>
							<span>1</span>
							<span>河南村镇银行多位储户称又被赋红码</span>
						</td>
						<td>
							<span>4</span>
							<span>通信行程卡时间范围由14天改为7天</span>
						</td>
					</tr>
					<tr>
						<td>
							<span>2</span>
							<span>约翰逊辞职后谁将接唐宁街烂摊子</span>
						</td>
						<td>
							<span>5</span>
							<span>山东新增66例本土无症状 在临沂</span>
						</td>
					</tr>
				</table>
			</div>

		</div>


		<!-- bottom -->
		<div class="bottom">
			<span>关于百度</span>
			<span>About Baidu</span>
			<span>京公网安备11000002000001号</span>
			<span>京ICP证030173号</span>
			<span>药品医疗器械网络信息服务备案(京)网药械信息备字(2021)第00159号</span>
			<span>^</span>
		</div>
	</body>
</html>
*{
	margin: 0;
	padding: 0;
}

.top{
	margin-top: 10px;
	width: 1200px;
	height: 30px;
}

.top_left{
	float: left;
}
.top_right{
	float: right;
}
a{
	/* 修改文字颜色 */
	color: black;
	/* 去掉超链接下划线 */
	text-decoration: none;
	font-size: 5px;
	margin-left: 20px;
}
.top_right > a,.top_right > input{
	margin-right: 10px;
}
/* 修改登录按钮 */
.top_right > input{
	width: 44px;
	height: 20px;
	background-color: rgba(61,83,239,0.9);
	color: white;
	border: 0px;
	border-radius: 6px;
	font-size: 1px;
}

.center{
	width: 1200px;
	text-align: center;
}

.center > div:nth-child(2) > .input_text{
	border: 2px solid lightgray;
	border-right: 0px;
	border-radius: 10px 0 0 10px;
	width: 450px;
	height: 30px;
	
}
.center > div:nth-child(2) > .button{
	border: 0px;
	border-radius: 0 10px 10px 0;
	background-color: rgba(61,83,239,0.9);
	color: white;
	width: 80px;
	height: 34px;
	margin-left: -6px;
}

table{
	width: 530px;
	text-align: left;
	margin-top: 30px;
	margin-left: 28%;
	font-size: 1px;
}
td{
	height: 30px;
}

.bottom{
	width: 1200px;
	text-align: center;
	font-size: 1%;
	position: absolute;
	bottom: 2px;
}

爬虫

requests — 请求页面,得到响应结果
BeaytifulSoup — 根据响应结果解析页面、提取结果

# 写入文件、数据库
import requests
from bs4 import BeautifulSoup

bs4模块能够从html或者xml中提取数据

for page in range(1, 11):
    print(f'这是第{page}页')
    URL = f'https://www.chinanews.com.cn/scroll-news/news{page}.html'

header = {} —> headers是一个字典:{key:value}
headers是给爬虫提供伪装的
User-Agent —> 将爬虫伪装为浏览器

    Headers = {
        'User-Agent':''
    }

    response = requests.get(url=URL)
    # 如果状态码 = 200,爬虫可用
    if response.status_code == 200:
        response.encoding = 'UTF-8'

为什么要对比打印结果和网页中的内容是否一致?

网页:分为静态页面和动态页面
静态页面:内容是写死的,除非人为的进行内容修改,否则这个页面的内容是一成不变的
动态页面:内容不是写死的,使用某种特殊的技术(JavaScript)使页面中的数据通过某种方式显示在页面中
requests得到的结果是静态页面的结果

  • BeautifulSoup(网页源码,解析器) —> 将字符串类型的源代码转换为BS4类型
  • bs4模块提供了一系列提取数据的方法,这些方法的操作对象的bs4类型的数据
 soup = BeautifulSoup(response.text, "html.parser")
  • select:根据CSS选择器(标签、class、id等)定位数据,
    得到的是符合这个选择器的所有结果(整体是列表,列表中每个元素是一个bs4类型的数据)
  • select_one:根据CSS选择器(标签、class、id等)定位数据,
    得到的是符合这个选择器的一个结果(是一个bs4类型数据)
  • text:从bs4类型数据中提取标签内的内容,结果为str
  • attrs:从bs4类型数据中提取标签内容属性值,结果为str
li_list = soup.select('body > div.w1280.mt20 > div.content-left > div.content_list > ul > li')
        # print(li_list)
        for i in li_list:
            if i.select_one('li > div.dd_lm > a') != None:
                news_type = i.select_one('li > div.dd_lm > a').text
                print(news_type)
                news_title = i.select_one('li > div.dd_bt > a').text
                print(news_title)
                news_href = 'https://www.chinanews.com.cn' + i.select_one('li > div.dd_bt > a').attrs['href']
                print(news_href)
                news_time = i.select_one('li > div.dd_time').text
                print(news_time)


生活
世界过敏性疾病日:长期熬夜可加重皮肤过敏
https://www.chinanews.com.cn/life/2022/07-08/9798520.shtml
7-8 10:08
生活
怎样改造老人的家才能适合老人家?
https://www.chinanews.com.cn/life/2022/07-08/9798515.shtml
7-8 10:07

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,异常处理是非常重要的一部分。当程序运行时如果出现错误,如果没有异常处理,程序就会崩溃。为了避免这种情况,Python提供了异常处理机制。 在Python中,异常处理语句使用 `try` 和 `except` 关键字来实现。`try` 语句块中包含可能会发生异常的代码,如果这段代码出现了异常,则会跳转到 `except` 语句块中执行异常处理代码。 下面是一个简单的例子: ```python try: num = int(input("请输入一个整数:")) print(10/num) except ZeroDivisionError: print("除数不能为0") except ValueError: print("输入的不是整数") ``` 在上面的代码中,我们尝试将用户输入的字符串转换为整数,并将其用作除数计算 10/num。如果用户输入的是 0,则会触发 ZeroDivisionError 异常。如果用户输入的不是整数,则会触发 ValueError 异常。如果发生异常,则会跳转到对应的 except 语句块中执行处理代码。 除了可以指定具体的异常类型,也可以使用 `except Exception` 来捕获所有异常。例如: ```python try: num = int(input("请输入一个整数:")) print(10/num) except Exception as e: print("发生异常:", e) ``` 在上面的代码中,如果发生任何异常,都会跳转到 `except` 语句块中执行处理代码,并将异常信息打印出来。 除了 `try` 和 `except`,还有 `finally` 关键字,它指定的代码块无论是否发生异常都会执行。例如: ```python try: num = int(input("请输入一个整数:")) print(10/num) except Exception as e: print("发生异常:", e) finally: print("程序执行完毕") ``` 在上面的代码中,无论是否发生异常,都会执行 `finally` 中的代码,即输出“程序执行完毕”。 总之,在Python中,异常处理是非常重要的一部分,它可以有效避免程序崩溃,提高程序的健壮性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值