用Python和javascript表单浏览网站

2024-09-28 20:49:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我没有太多从网站上抓取数据的经验。我通常使用Python的“requests”和“BeautifulSoup”。你知道吗

我需要从这里下载这个表https://publons.com/awards/highly-cited/2019/ 我通常使用右键单击和检查,但格式不是我习惯使用的格式。我读了一点书,看起来像是Javascript,我可以从https://publons.com/static/cache/js/app-59ff4a.js中提取数据。我读过其他推荐Selenium和PhantomJS的帖子。但是,我不能修改路径,因为我不是这台计算机的管理员(我使用的是Windows)。你知道怎么解决这个问题吗?如果Python不是一个选项,那么很高兴使用R。你知道吗

谢谢!你知道吗


Tags: 数据httpscom网站格式js经验requests
1条回答
网友
1楼 · 发布于 2024-09-28 20:49:05

如果您通过dev工具监视web流量,您将看到页面为更新内容而进行的API调用。返回的信息是json格式的。你知道吗

例如:第1页

import requests

r = requests.get('https://publons.com/awards/api/2019/hcr/?page=1&per_page=10').json()

您可以在循环中更改page参数以获得所有结果。你知道吗

结果的总数已经在第一个响应中通过r['count']表示了,这样就很容易计算出每页循环10个结果的页面。只是在你提出要求的时候一定要有礼貌。你知道吗

大纲:

import math, requests

with requests.Session() as s:
    r = s.get('https://publons.com/awards/api/2019/hcr/?page=1&per_page=10').json()
    #do something with json. Parse items of interest into list and add to a final list? Convert to dataframe at end?
    number_pages = math.ceil(r['count']/10)

    for page in range(2, number_pages + 1):
        #perhaps have a delay after X requests
        r = s.get(f'https://publons.com/awards/api/2019/hcr/?page={page}&per_page=10').json()
        #do something with json. Parse items of interest into list and add to a final list? Convert to dataframe at end?

相关问题 更多 >