gpt4 book ai didi

python - 网站列表解析器只检索20个项目,如何让网站加载更多

转载 作者:太空宇宙 更新时间:2023-11-03 15:33:17 25 4
gpt4 key购买 nike

这是网站Forbes Most Admired Companies包含 50 家公司的列表,我正在尝试解析该列表并将其导出到 csv 文件

我的代码只得到了 20,因为当你向下滚动时页面会加载。有没有办法模拟向下滚动或使其完全加载?

from lxml import html
import requests

def schindler(max): # create a list of the companies
page = requests.get('http://beta.fortune.com/worlds-most-admired-companies/list/')
tempContainer = html.fromstring(page.content)
names = []
position = 1

while position <= max:
names.extend(tempContainer.xpath('//*[@id="pageContent"]/div[2]/div/div/div[1]/div[1]/ul/li['+str(position)+']/a/span[2]/text()'))
position = position + 1

return names

(这只是列表创建,.csv 导出器没有问题)

然后我将其打印到 chek,列表中仅出现 20 项

print(schindler(50))

最佳答案

您似乎能够以 JSON 形式获取数据。网址中的 20 似乎是开始的排名,30 是项目的数量。

示例代码:

url = "http://fortune.com/api/v2/list/1918408/expand/item/ordering/asc/20/30"

resp = requests.get(url)
for entry in resp.json()['list-items']:
print(entry['rank'], entry['name'])

关于python - 网站列表解析器只检索20个项目,如何让网站加载更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42739980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com