python爬虫爬取洛阳师范学院官网代码
时间: 2023-09-15 17:17:48 浏览: 52
好的,以下是使用 Python 爬虫爬取洛阳师范学院官网代码的示例代码:
```python
import requests
url = "http://www.lynu.edu.cn/"
response = requests.get(url)
if response.status_code == 200:
print(response.text)
else:
print("请求失败")
```
以上代码使用了 requests 库发送了一个 GET 请求,获取了洛阳师范学院官网的代码,如果请求成功,则打印出网页源代码,否则提示请求失败。您可以将以上代码保存为一个 .py 文件,然后在终端中运行该文件,即可看到网页源代码。
相关问题
python爬虫爬取豆瓣数据源代码
以下是一个简单的Python爬虫爬取豆瓣电影Top250的代码示例:
```
import requests
from bs4 import BeautifulSoup
# 发送请求
url = 'https://movie.douban.com/top250'
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.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 解析数据
movies = soup.find_all('div', class_='info')
for movie in movies:
title = movie.find('span', class_='title').text.strip()
score = movie.find('span', class_='rating_num').text.strip()
quote = movie.find('span', class_='inq').text.strip()
print('电影名:', title)
print('评分:', score)
print('评价:', quote)
# 相关问题
1. 你可以解释一下代码中的requests和BeautifulSoup库吗?
2. 为什么要添加headers头部信息?
3. 如何将爬取到的数据保存到本地?
python爬虫爬取天气数据完整代码
以下是一个基于Python的爬虫爬取天气数据的完整代码示例:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求获取网页内容
url = "https://www.example.com/weather" # 替换为你要爬取的网页地址
response = requests.get(url)
html_content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, "html.parser")
# 提取需要的天气数据
weather_data = []
weather_elements = soup.select("div.weather-info") # 替换为你要提取的天气数据的CSS选择器
for element in weather_elements:
date = element.select_one("span.date").text
temperature = element.select_one("span.temperature").text
description = element.select_one("span.description").text
weather_data.append({"date": date, "temperature": temperature, "description": description})
# 打印天气数据
for data in weather_data:
print("日期:", data["date"])
print("温度:", data["temperature"])
print("描述:", data["description"])
print()
```
请注意,上述代码仅为示例,实际使用时需要根据目标网页的结构和需要提取的数据进行相应的修改。