【发布时间】:2021-02-21 05:34:11
【问题描述】:
我尝试使用 Python 创建我的第一个脚本。我正在使用 Splash 和 BS4。
我按照约翰·沃森·鲁尼 (John Watson Rooney) 的教程学习(但有自己的目标):How I Scrape JAVASCRIPT websites with Python
我的目标是抓取这个网站调查:Best movies of 2020
这是我的问题:它多次呈现相同的标题,但列表中最多有 6 个重复项,没有任何逻辑顺序。有时渲染不到 100 行,有时更多?
我想要什么:
- 获取 100 个标题,按顺序
- 以 .csv 格式导出。
这是我的代码:
import requests
import csv
from bs4 import BeautifulSoup
url = 'https://www.senscritique.com/top/resultats/Les_meilleurs_films_de_2020/2582670'
r = requests.get('http://localhost:8050/render.html',
params={'url': url, 'wait': 2})
soup = BeautifulSoup(r.text, 'html.parser')
podium = soup.find_all('li', class_="elpo-item")
podium_list = []
for titres in podium:
for titles in soup.find_all('h2'):
podium_list.append(titles.text)
for liste in podium_list:
print(liste)
问题:
- 如何只抓取 100 个标题?我错过了什么?
- 我的代码是否正确,如何优化?
- Splash 真的很适合我使用吗,还是有其他更简单的库来抓取 JS 网站?
对于 .csv 部分,我现在将自己尝试,但如果您有任何提示,我当然会听到!
感谢您的帮助。
【问题讨论】:
-
这是“废品”,而不是“废品”。 “报废”是您对不再可修理的旧车所做的处理。
-
对不起,我不是英语本地人。谢谢指正。
标签: python web-scraping beautifulsoup