【问题标题】:How to find all ieee article pages to scrape using BeautifulSoup (Just all article URLs automatically)如何使用 BeautifulSoup 查找所有要抓取的 ieee 文章页面(自动搜索所有文章 URL)
【发布时间】:2020-02-16 04:15:14
【问题描述】:

我想找到所有的文章页面和 scraping 标题和描述标签,首先我的问题是找到所有文章,要找到我们需要在搜索框中搜索的文章,但是我想自动做到这一点。 我认为其中一种方法可能是使用站点地图,但我不确定。 请帮我想办法。

我知道如何抓取网站,但在这种情况下,我的问题是如何在搜索框中不搜索并在 ieee 中自动查找所有文章(例如 https://ieeexplore.ieee.org/)。

我想获取https://ieeexplore.ieee.org 中的所有文章页面(仅获取所有文章页面 URL)。

【问题讨论】:

    标签: web-scraping beautifulsoup scrapy


    【解决方案1】:

    当你点击搜索按钮时,它会将你重定向到一个链接

    https://ieeexplore.ieee.org/search/searchresult.jsp?newsearch=true&queryText=smart%20grid
    

    queryText 参数是您的搜索词。内容是使用 JavaScript 加载的,因此您不能只向链接发送请求然后解析响应。你可以

    • 使用 selenium 转到链接
    • 模拟该页面上使用的 XHR 请求

    使用 Selenium,转到 url(使用您的首选搜索词),单击加载更多按钮,直到您加载了足够的文章,然后得到响应。

    我更喜欢模拟 XHR 请求,因为它更快。

    import requests
    # change this for a different search term
    search_term = "smart grid"
    # change this for different page no
    page_no = 1
    
    headers = {
        "Accept": "application/json, text/plain, */*",
        "Origin": "https://ieeexplore.ieee.org",
        "Content-Type": "application/json",
    }
    payload = {
        "newsearch": True,
        "queryText": search_term,
        "highlight": True,
        "returnFacets": ["ALL"],
        "returnType": "SEARCH",
        "pageNumber": page_no
    }
    r = requests.post(
            "https://ieeexplore.ieee.org/rest/search",
            json=payload,
            headers=headers
        )
    page_data = r.json()
    for record in page_data["records"]:
        print(record["articleTitle"])
        print('https://ieeexplore.ieee.org'+record["documentLink"], end="\n----\n")
    
    

    输出

    IEEE Vision for Smart Grid Controls: 2030 and Beyond Reference Model
    https://ieeexplore.ieee.org/document/6598993/
    ----
    IEEE Vision for Smart Grid Control: 2030 and Beyond Roadmap
    https://ieeexplore.ieee.org/document/6648362/
    ----
    Software models for Smart Grid
    https://ieeexplore.ieee.org/document/6225717/
    ----
    IEEE Vision for Smart Grid Communications: 2030 and Beyond Roadmap
    https://ieeexplore.ieee.org/document/6690098/
    ----
    IEEE Smart Grid Vision for Computing: 2030 and Beyond Roadmap
    https://ieeexplore.ieee.org/document/7376995/
    ----
    ...
    

    JSON 格式的响应,我们可以使用 python 解析得到你想要的输出。 URL https://ieeexplore.ieee.org/rest/search 可以使用浏览器开发工具上的 network 选项卡获取。

    【讨论】:

    • 太棒了!很好的答案。根据您的出色回答,我有一个新问题,那就是当我使用 XHR 请求时,如何获得更多结果?我需要为更多按钮做点什么,或者我们不需要像 selenium 方法那样点击更多按钮来获得更多结果?
    • @WilliamJohnson 您可以使用 for 循环并更改 page_no
    • 是的,看来我需要尝试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多