【发布时间】:2021-09-13 11:22:03
【问题描述】:
我正在尝试编写 ansible playbook 来抓取网站,然后将其内容存储到 aws s3 存储桶下的静态文件中。这是爬虫代码:
"""
Handling pages with the Next button
"""
import sys
from urllib.parse import urljoin
import requests
from bs4 import BeautifulSoup
url = "https://xyz.co.uk/"
file_name = "web_content.txt"
while True:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
raw_html = soup.prettify()
file = open(file_name, 'wb')
print('Collecting the website contents')
file.write(raw_html.encode())
file.close()
print('Saved to %s' % file_name)
#print(type(raw_html))
# Finding next page
next_page_element = soup.select_one('li.next > a')
if next_page_element:
next_page_url = next_page_element.get('href')
url = urljoin(url, next_page_url)
else:
break
这是我的 ansible-playbook:
---
- name: create s3 bucket and upload static website content into it
hosts: localhost
connection: local
tasks:
- name: create a s3 bucket
amazon.aws.aws_s3:
bucket: testbucket393647914679149
region: ap-south-1
mode: create
- name: create a folder in the bucket
amazon.aws.aws_s3:
bucket: testbucket393647914679149
object: /my/directory/path
mode: create
- name: Upgrade pip
pip:
name: pip
version: 21.1.3
- name: install virtualenv via pip
pip:
requirements: /root/ansible/requirements.txt
virtualenv: /root/ansible/myvenv
virtualenv_python: python3.6
environment:
PATH: "{{ ansible_env.PATH }}:{{ ansible_user_dir }}/.local/bin"
- name: Run script to crawl the website
script: /root/ansible/beautiful_crawl.py
- name: copy file into bucket folder
amazon.aws.aws_s3:
bucket: testbucket393647914679149
object: /my/directory/path/web_content.text
src: web_content.text
mode: put
问题是当我运行它时,它运行良好,直到任务 name: install virtualenv via pip 然后在执行任务时抛出以下错误 name: Run script to crawl the website:
致命:[本地主机]:失败! => {“更改”:true,“msg”:“非零返回码”,“rc”:2,“stderr”:“/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-9798 3643645466 /beautiful_crawl.py: line 1: import: command not found\n/root/.ansible /tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py: line 2: from: command not found\n/root /.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 3 行:导入:找不到命令\n/roo t/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_cra wl.py:第 4 行:来自:找不到命令\n/root/.ansible/tmp/ansible-tmp-162513770 0.8854306-13026-9798346 /beautiful_crawl.py:第 6 行:url:找不到命令 d\n/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 7 行:file_name:找不到命令\n/根/.ansible/tmp/ansible-t mp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py:第 10 行:意外标记附近的语法错误
('\n/root/.ansible/tmp/ansible-tmp-1625137700.885430 6-13026-97983643645466/beautiful_crawl.py: line 10:response = requests.get (url)'\n", "stderr_lines": ["/root/.ansible/tmp /ansible-tmp-1625137700.8854306-1 3026-97983643645466/beautiful_crawl.py:第 1 行:导入:找不到命令”,“/ro ot/.ansible/tmp/ansible-tmp-1625137700.8854306-13026-979836436456 : 第 2 行:来回m:找不到命令”,“/root/.ansible/tmp/ansible-tmp-162513 7700.8854306-13026-97983643645466/beautiful_crawl.py:第3行:导入:找不到命令”,“/root/.ansible/tmp /ansible-tmp-1625137700.8854306-13026-9798364364546 6/beautiful_crawl.py:第 4 行:发件人:找不到命令”,“/root/.ansible/tmp/ansi ble-tmp-1625137700.8854306-13026-979836_crawl4566第 6 行:url:找不到命令”、“/root/.ansible/tmp/ansible-tmp-1625137700.8854306-13 026-97 983643645466/beautiful_crawl.py:第 7 行:文件名:找不到命令”、“/root/. ansible/tmp/ansible-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl。 py:第 10 行:意外标记附近的语法错误('", "/root/.ansible/tmp/ansibl e-tmp-1625137700.8854306-13026-97983643645466/beautiful_crawl.py: line 10:response = requests.get(url)'"], "stdout": "", "stdout_lines": []}
我在这里做错了什么?
【问题讨论】:
标签: python-3.x ansible scripting