如何使用Python刮取web

2024-09-29 23:31:13 发布

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

我想第一次做网页抓取一些网站,是在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')

Tags: httpsimporturl网站mywwwpagerequests
1条回答
网友
1楼 · 发布于 2024-09-29 23:31:13

试试这个:

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文件中的内容:

enter image description here

相关问题 更多 >

    热门问题