爬虫软件python 源代码
时间: 2023-10-10 08:10:43 浏览: 49
这里提供一个简单的 Python 爬虫代码示例,可以爬取一个网站的所有页面并保存为 HTML 文件:
```python
import requests
from bs4 import BeautifulSoup
# 指定要爬取的网站
url = 'https://www.example.com'
# 发送请求并获取响应
response = requests.get(url)
# 解析 HTML 页面
soup = BeautifulSoup(response.text, 'html.parser')
# 获取所有页面链接
links = soup.find_all('a')
# 遍历链接并爬取
for link in links:
href = link.get('href')
if href.startswith('http'):
sub_response = requests.get(href)
filename = href.split('/')[-1]
with open(filename, 'w') as f:
f.write(sub_response.text)
```
以上代码使用了 requests 库发送 HTTP 请求,使用 BeautifulSoup 库解析 HTML 页面,并通过遍历页面中的链接来爬取其他页面。这只是一个简单的示例,实际的爬虫可能需要更复杂的逻辑和数据处理。同时,需要注意合法使用爬虫,遵守网站的 robots.txt 规则。
相关问题
爬虫python源代码
为了遵守规则3,我将只提供一个简单的Python爬虫代码,该代码可以从指定的URL中获取HTML内容。请注意,爬虫的使用需要遵守相关法律法规,不得用于非法用途。
```python
import requests
url = 'https://www.example.com'
response = requests.get(url)
html_content = response.text
print(html_content)
```
python爬虫四大名著源代码
为了遵守规则3,我将只使用引用中提供的信息来回答您的问题。
以下是一个简单的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 = 'http://www.xiangcunxiaoshuo.com/hlm/'
response = requests.get(url, headers=headers)
# 解析HTML
soup = BeautifulSoup(response.text, 'html.parser')
chapter_list = soup.find_all('div', class_='book_list')[0].find_all('a')
# 获取所有章节链接
chapter_links = []
for chapter in chapter_list:
chapter_links.append(chapter['href'])
# 获取每个章节的内容
for link in chapter_links:
response = requests.get(link, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
content = soup.find_all('div', class_='book_content')[0].text
print(content)
time.sleep(1) # 防止被封IP
```