繁体   English   中英

使用 Python 和 BeautifulSoup 发布抓取

[英]Issue Scraping with Python and BeautifulSoup

我已经抓取这个网站一年了,但是他们最近改变了网站的布局,出于某种原因我不能再让它工作了。 我正在使用 Python 和 BeautifulSoup。

我基本上是想从此链接获取表格中的数据: https://www.loto.ro/?p=3872

这是用于旧布局的代码,我针对网站的当前布局对其进行了调整:

            website_result = requests.get("https://www.loto.ro/?p=3872")
            src = website_result.content
            soup = BeautifulSoup(src, 'lxml')

            for i in range(0, 8):
                table_title = soup.select(".content .content-info .rezultate-extrageri-content.resultDiv .button-open-details")[i].get_text().strip()

                if "6/49" in table_title:
                    images = soup.select(".content-info .rezultate-extrageri-content.resultDiv "
                                         ".info-rezultat .numere-extrase img[src]")

                    if len(images) > 0:
                        table = soup.select(".content .content-info .rezultate-extrageri-content.resultDiv .results-table")[i]

在调试模式下,我的代码卡在“table_title”行,没有给我任何错误或回溯,所以我什至不知道问题出在哪里。

有任何想法吗? 谢谢。

URL 结果即将到来的形式确实是新的,因为其中包含“newLottoSite”。

尝试这个:

import pandas as pd
import requests
from tabulate import tabulate

new_url = "https://www.loto.ro/loto-new/newLotoSiteNexioFinalVersion/web/app2.php/jocuri/649_si_noroc/rezultate_extragere.html"

headers = {
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.41 Safari/537.36",
    "referer": "https://www.loto.ro/?p=3872",
}

df = pd.read_html(requests.get(new_url, headers=headers).text, flavor="lxml")[0]
print(tabulate(df, headers="keys", tablefmt="psql"))

这应该是 output:

+----+---------------------------------------+---------------------------------------+---------------------------------------+---------------------------------------+
|    | CAT.                                  | Numar castiguri                       | Valoare castig                        | Report                                |
|----+---------------------------------------+---------------------------------------+---------------------------------------+---------------------------------------|
|  0 | I (6/6)                               | REPORT                                | 272.80920                             | 4.289.31280                           |
|  1 | II (5/6)                              | 5                                     | 18.18728                              | -                                     |
|  2 | III (4/6)                             | 285                                   | 31907                                 | -                                     |
|  3 | IV (3/6)                              | 4.563                                 | 3000                                  | -                                     |
|  4 | Fond total de castiguri: 4.608.075,60 | Fond total de castiguri: 4.608.075,60 | Fond total de castiguri: 4.608.075,60 | Fond total de castiguri: 4.608.075,60 |
+----+---------------------------------------+---------------------------------------+---------------------------------------+---------------------------------------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM