我已经刮了这个网站一年了,但是他们最近改变了网站的布局,由于某种原因我不能让它工作了。我正在使用 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"))
这应该输出:
+----+---------------------------------------+---------------------------------------+---------------------------------------+---------------------------------------+
| | 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 |
+----+---------------------------------------+---------------------------------------+---------------------------------------+---------------------------------------+
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句