Python 写一个网络采集程序
时间: 2023-06-13 07:08:35 浏览: 68
好的,以下是一个简单的 Python 网络采集程序的示例代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取网页标题
title = soup.title.string
print('网页标题:', title)
# 获取所有链接
links = soup.find_all('a')
for link in links:
print('链接:', link['href'])
```
这个程序使用了 requests 库发送 HTTP 请求,并使用 BeautifulSoup 库解析 HTML。它能够获取网页的标题和所有链接。需要注意的是,这个程序只是一个示例,实际应用中可能需要根据具体需求进行更改和优化。
相关问题
使用python写一个图书查询
好的,这里提供一个简单的图书查询程序的Python代码,你可以根据需要进行修改和完善:
```python
books = {"Python编程从入门到实践": {"author": "Eric Matthes", "year": 2016},
"流畅的Python": {"author": "Luciano Ramalho", "year": 2015},
"Python基础教程": {"author": "Magnus Lie Hetland", "year": 2007},
"Python网络数据采集": {"author": "Mitchell Ryan", "year": 2017}}
while True:
keyword = input("请输入要查询的图书名称:")
if keyword == "exit":
print("程序已退出!")
break
elif keyword in books:
print("图书名称:", keyword)
print("图书作者:", books[keyword]["author"])
print("出版年份:", books[keyword]["year"])
else:
print("图书不存在,请重新输入!")
```
该程序通过一个字典存储了几本图书的信息,用户可以输入图书名称进行查询。如果查询到了相关图书,程序会输出该图书的作者和出版年份;如果没有找到,程序会提示用户重新输入。当用户输入“exit”时,程序会退出。
用Python写一一个温度采集系统,列出代码
好的,以下是用Python写的一个温度采集系统的示例代码:
```python
import time
import board
import adafruit_dht
# 设置传感器类型和引脚
sensor = adafruit_dht.DHT11(board.D4)
# 读取温湿度值
while True:
try:
temperature_c = sensor.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = sensor.humidity
print("温度: {:.1f}℃ / {:.1f}℉,湿度: {}%".format(temperature_c, temperature_f, humidity))
except RuntimeError as error:
print(error.args[0])
time.sleep(2)
```
在这个示例代码中,我们使用了Adafruit提供的DHT传感器库,并连接了一个DHT11温湿度传感器到树莓派(或其他支持GPIO的开发板)的引脚D4上。在主循环中,我们读取温度和湿度值,并将其输出到控制台。程序将每隔两秒钟读取一次传感器。请注意,在实际应用中,我们可能需要将数据存储到数据库或通过网络发送给其他设备。