嘿,我创建了一个脚本来对特定网站进行数据抓取,代码没问题,但是当我尝试执行代码时出现错误。
尝试使用 cmd 来执行文件,这样我就可以得到结果,但仍然是一个错误。
NameError: name 'scrape' is not defined
''scrape.py'' 文件“”,第 1 行
import requests
from bs4 import BeautifulSoup
url = "sawadee.nl/groepsrondreizen/midden-en-zuid-amerika/costa-rica"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
for row in soup.select(".departure-list__table tbody tr"):
date = row.select_one(".departure-list__date").text.strip()
group = row.select_one(".departure-list__group").text.strip()
print(f"Op {date} is de groepssamenstelling: {group}")
你是如何运行脚本的?
如果你从终端运行它,你必须运行这样的东西:
python file_name.py
我尝试从 cmd / python 运行它,因为我使用 Pycharm 创建脚本。脚本的结果是一个列表,其中包含我在 excel 文件或 csv 中需要的信息。
我将脚本运行为:python scrape.py 那失败了。
我尝试运行您的相同代码,但通过在 url 前面添加 https 进行了小改动。我在响应中得到
200
但在 soup.select
它由于一些 html 解析而失败。您可能需要检查一下,因为我在任何 html 标签中都没有看到出发列表:
import requests
from bs4 import BeautifulSoup
url = "https://sawadee.nl/groepsrondreizen/midden-en-zuid-amerika/costa-rica"
response = requests.get(url). ### getting 200 response here
soup = BeautifulSoup(response.text, "html.parser")
for row in soup.select(".departure-list__table tbody tr"):
date = row.select_one(".departure-list__date").text.strip()
group = row.select_one(".departure-list__group").text.strip()
print(f"Op {date} is de groepssamenstelling: {group}")