写一段有趣的python代码
时间: 2023-04-10 11:01:36 浏览: 85
当然,我可以回答这个问题!以下是一段有趣的 Python 代码,可以生成一个随机的笑话:
```python
import random
jokes = [
"Why did the tomato turn red? Because it saw the salad dressing!",
"Why did the chicken cross the playground? To get to the other slide!",
"Why did the cookie go to the doctor? Because it was feeling crumbly!",
"Why did the banana go to the doctor? Because it wasn't peeling well!",
"Why did the coffee file a police report? It got mugged!",
]
print(random.choice(jokes))
```
每次运行这个程序,它都会随机选择一个笑话并输出。希望你喜欢!
相关问题
请写一段有趣的Python代码
当然,我很乐意为您展示一段有趣的Python代码:
```python
import antigravity
# Yes, that's it!
```
这段代码调用了 Python 的一个 easter egg,它会打开你的默认浏览器并加载官方 Python 2.7 文档中的一页漫画,其中超人正在用 Python 超能力飞行。这是一个不错的小彩蛋,可以让你和朋友感到惊喜。
写一段爬虫python代码
好的,以下是一个简单的 Python 网络爬虫的示例代码,使用 requests 和 BeautifulSoup 库:
```python
import requests
from bs4 import BeautifulSoup
# 设置请求头,模拟浏览器请求
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 发送 GET 请求
url = 'http://www.example.com'
response = requests.get(url, headers=headers)
# 解析 HTML
soup = BeautifulSoup(response.text, 'html.parser')
# 获取标签内容
print(soup.title.string)
```
这段代码会向 `http://www.example.com` 发送 GET 请求,然后使用 BeautifulSoup 库解析 HTML 页面,并输出页面标题的内容。需要注意的是,如果你想爬取其他网站的内容,需要遵守该网站的爬虫规则,避免触犯相关法律法规。