用python写一段网页爬虫代码
时间: 2023-04-05 07:00:21 浏览: 98
你好,我是 C 知道,以下是回答:
可以使用 Python 的 requests 和 BeautifulSoup 库来编写一个简单的网页爬虫。首先,使用 requests 库发送 HTTP 请求获取网页内容,然后使用 BeautifulSoup 库解析 HTML 标签,提取所需的信息。
以下是一个示例代码:
```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.get('href'))
```
当然,具体的爬虫代码需要根据不同的网站和需求进行调整。
相关问题
用python写一段网页爬虫代码 爬取电影
以下是一个简单的 Python 代码片段,可以爬取电影网站的电影信息:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.imdb.com/chart/top'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
movies = soup.select('td.titleColumn')
crew = [a.attrs.get('title') for a in soup.select('td.titleColumn a')]
ratings = [b.attrs.get('data-value') for b in soup.select('td.posterColumn span[name="ir"]')]
for i in range(0, len(movies)):
print(f"{i+1}. {movies[i].text.strip()} ({crew[i]}) - Rating: {ratings[i]}")
```
这个代码片段使用 requests 库发送 HTTP 请求并获取网页内容,然后使用 BeautifulSoup 库解析 HTML 代码。它从 IMDb 网站中获取了电影的标题、演职人员和评级,并将它们打印在控制台中。你可以根据需要进行修改和优化。
用python写一段网页爬虫代码 爬取电影 带注释
以下是一个简单的Python爬虫代码,可以爬取电影信息:
``` 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'}
# 设置目标网站URL
url = "https://movie.douban.com/top250"
# 发送请求并获取响应
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 找到所有电影条目的div
movie_list_div = soup.find('ol', class_='grid_view')
# 遍历每个电影条目,提取信息
for movie_li in movie_list_div.find_all('li'):
# 获取电影排名
rank = movie_li.find('em').text
# 获取电影名称和电影详情页URL
movie_name = movie_li.find('span', class_='title').text
movie_url = movie_li.find('a')['href']
# 获取电影评分和评价人数
rating = movie_li.find('span', class_='rating_num').text
rating_num = movie_li.find('div', class_='star').find_all('span')[3].text
# 输出电影信息
print(f"排名:{rank}")
print(f"电影名:{movie_name}")
print(f"电影详情页URL:{movie_url}")
print(f"评分:{rating}")
print(f"评价人数:{rating_num}")
print("==============================")
```
代码解释:
1. 导入必要的库 `requests` 和 `BeautifulSoup`。
2. 设置请求头,模拟浏览器发送请求。
3. 设置目标网站的URL。
4. 发送请求并获取响应,使用 `BeautifulSoup` 解析响应内容。
5. 找到电影条目的 `div` 元素。
6. 遍历每个电影条目,提取需要的电影信息。
7. 输出电影信息。
这段代码可以爬取豆瓣电影Top250的电影信息。你可以根据自己的需求修改代码来爬取其他网站的电影信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)