python爬虫


import requests
url = "https://www.google.cn/"
for i in range(20):
try:
r = requests.get(url, timeout = 30)
r.raise_for_status()
r.encoding = 'utf-8'
print(r.text)
except:
print('')
#(3)
import requests
from bs4 import BeautifulSoup
a = '''<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<h1>我的第一个标题</h1>
<p id="first">我的第一个段落。</p>
</body>
<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>
</html>'''
soup = BeautifulSoup(a)
print(soup.head,25)
print(soup.body)
print(soup.p)
print(soup.title.string,soup.h1.string,soup.p.string)

 
 
posted @   徐嘉俊  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示