【问题标题】:How can I scrape web in Python如何在 Python 中抓取网页
【发布时间】:2021-02-21 13:20:10
【问题描述】:

我想第一次做网页抓取一些用 JS 创建的网站。但我不知道如何在开发人员工具上找到表格。 这是我的网站:https://www.money.pl/gielda/gpw/akcje/

还有我的代码:

import requests
from bs4 import BeautifulSoup as soup

my_url = "https://www.money.pl/gielda/gpw/akcje/"
headers = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36")
page = requests.get(my_url, headers=headers, timeout=10)
page_soup = soup(page.text, 'lxml')

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    试试这个:

    import csv
    
    import requests
    from bs4 import BeautifulSoup
    from tabulate import tabulate
    
    page = requests.get("https://money.pl/gielda/gpw/akcje/")
    soup = BeautifulSoup(
        page.content, "html.parser",
    ).find_all("div", class_="rt-tr-group")
    
    akcje_gpw = [
        [
            i.getText(strip=True) for i in div.find("div") if i.getText()
        ] for div in soup
    ]
    
    columns = [
        "Walor", "Kurs PLN", "Zmiana (%)", "Otwarcie",
        "Min", "Max", "Obrót (szt.)", "Obrót (PLN)",
        "Czas aktualizacji",
    ]
    
    print(tabulate(akcje_gpw[:10], headers=columns))
    
    with open("akcje_tabela.csv", "w") as f:
        w = csv.writer(f)
        w.writerow(columns)
        w.writerows(akcje_gpw)
    
    

    输出:

    Walor                         Kurs PLN    Zmiana (%)    Otwarcie    Min     Max     Obrót (szt.)    Obrót (PLN)    Czas aktualizacji
    ----------------------------  ----------  ------------  ----------  ------  ------  --------------  -------------  -------------------
    11 bit studios SA             485,00      -2,61         492,00      485,00  499,50  4 512           2 213 838      2021-01-29
    3R Games SA                   1,03        -0,96         1,07        1,03    1,07    11 757          12 482         2021-01-29
    4Fun Media SA                 6,48        +2,86         6,76        6,30    6,76    6 618           42 777         2021-01-29
    AB SA                         31,40       +0,64         31,10       31,10   31,80   10 633          335 017        2021-01-29
    AC SA                         35,00       0,00          35,50       34,80   35,50   3 080           107 836        2021-01-29
    Action SA w restrukturyzacji  5,92        -0,67         6,06        5,84    6,06    14 456          86 121         2021-01-29
    Adiuvo Investments SA         4,90        +4,26         4,85        4,83    4,96    8 865           43 344         2021-01-29
    Agora SA                      6,84        -0,87         6,86        6,84    6,94    13 001          89 663         2021-01-29
    Agroton Plc                   7,16        +0,85         7,10        6,80    7,18    31 773          223 314        2021-01-29
    Ailleron SA                   11,60       -0,85         11,55       11,30   11,80   7 487           86 225         2021-01-29
    ----------------------------  ------  -----  ------  ------  ------  ------  ---------  ----------
    

    这是.csv 文件中的内容:

    【讨论】:

    • 非常感谢。请让我知道你是如何在网上找到它的(“div”,class_="rt-tr-group")。或者你在 chrome 上的 Dev.tools 上哪里可以找到?
    • 关闭该站点的JS 并再次检查源。或者您可以使用 Ctrl+Shift+C 选择元素。
    • 它看起来很奇怪,但我看到 ModuleNotFoundError: No module named 'tabulate' 我不能 pip install tabulate
    • 然后只需删除导入和这一行:print(tabulate(akcje_gpw[:10], headers=columns))
    猜你喜欢
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 2021-01-12
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    相关资源
    最近更新 更多