python爬虫递归爬取url
时间: 2023-11-12 17:44:15 浏览: 58
可以使用递归函数实现,以下是一个示例代码:
```python
import requests
from bs4 import BeautifulSoup
def crawl(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
print("正在爬取:", url)
# 在这里可以对页面进行解析,提取信息等操作
for link in soup.find_all('a'):
new_url = link.get('href')
if new_url and new_url.startswith('http'): # 只爬取以http开头的链接
crawl(new_url) # 递归调用
if __name__ == '__main__':
start_url = 'https://www.example.com'
crawl(start_url)
```
注意要加上判断条件,避免爬取到重复的链接或者死循环。另外,爬取网站时需要遵守相关法律法规和网站的规定,不得进行非法爬取或者恶意攻击。
相关问题
使用python爬虫爬取豆瓣电影top250
以下是使用Python爬虫爬取豆瓣电影Top250的代码:
```python
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'}
movie_list = []
def get_movie(url):
res = requests.get(url, headers=headers)
soup = BeautifulSoup(res.text, 'html.parser')
movies = soup.select('.item')
for movie in movies:
title = movie.select('.title')[0].text
link = movie.select('.hd a')[0]['href']
rating = movie.select('.rating_num')[0].text
comment = movie.select('.quote span')[0].text if movie.select('.quote span') else ''
movie_list.append({'title': title, 'link': link, 'rating': rating, 'comment': comment})
next_page = soup.select('.next a')
if next_page:
get_movie(next_page[0]['href'])
get_movie(url)
for movie in movie_list:
print(movie['title'], movie['rating'], movie['comment'], movie['link'])
```
这个代码使用了requests和BeautifulSoup库,首先定义了一个URL和header,然后定义了一个空的电影列表。get_movie函数用来从每个页面中获取电影信息,包括电影名称、链接、评分和评论。如果有下一页,它会递归地调用自己,直到获取所有的电影信息。最后,通过循环遍历电影列表,输出每个电影的名称、评分、评论和链接。
python爬虫爬取某一节点下的所有数据
要爬取某一节点下的所有数据,需要使用递归函数来遍历整个节点树。以下是一个示例代码,可以爬取一个网站上所有的文章标题和链接:
```python
import requests
from bs4 import BeautifulSoup
def get_links(url):
# 获取页面内容
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
# 获取所有的文章标题和链接
articles = soup.find_all('article')
for article in articles:
title = article.find('h2').text.strip()
link = article.find('a')['href']
print(title, link)
# 递归遍历子节点
next_page = soup.find('a', {'class': 'next'})
if next_page:
next_url = next_page['href']
get_links(next_url)
# 从首页开始爬取
get_links('https://example.com/articles/')
```
这个函数首先获取一个页面的内容,然后使用 BeautifulSoup 找到所有的文章节点,并提取出标题和链接。接下来它查找下一页的链接,如果有的话,就递归调用自己来遍历下一页。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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_ask_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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)