如何在美丽的汤中导航特定标签?

How to navigate specific tag in beautiful soup?

我想浏览网站上的特定标签。在这个网站上,很少有像这样的标签 我只想浏览其中一个。 每次我 运行 代码我都会得到不同的输出。

import bs4 as bs
import urllib

source = urllib.urlopen("https://taripebi.ge/%E1%83%91%E1%83%94%E1%83%9C%E1%83%96%E1%83%98%E1%83%9C%E1%83%98%E1%83%A1-%E1%83%A4%E1%83%90%E1%83%A1%E1%83%94%E1%83%91%E1%83%98").read()
soup = bs.BeautifulSoup(source, 'lxml')

for paragraph in soup.find('div', style = "width: 40%;/* float: left; */"):
    print(paragraph)

每次我 运行 代码都会得到不同的输出。

是的。每次页面返回不同的结果。即使您的选择是错误的,也不能解释您每次都打印出不同的结果。我 运行 这几次,每次都得到不同的结果。

from bs4 import BeautifulSoup
import requests
import pandas as pd
r = requests.get("https://taripebi.ge/%E1%83%91%E1%83%94%E1%83%9C%E1%83%96%E1%83%98%E1%83%9C%E1%83%98%E1%83%A1-%E1%83%A4%E1%83%90%E1%83%A1%E1%83%94%E1%83%91%E1%83%98")
df=pd.read_html(r.text)
print(df)

产出

运行第一名

[    0       1       2       3       4       5        6      7
0 NaN   ---00  2.4992  2.5700    2.64    2.63  2.59100  ---00
1 NaN   ---00  2.3593  2.4800    2.58   ---00     2.53  ---00
2 NaN   ---00  2.0493  2.2495   ---00  2.0500   2.2400  ---00
3 NaN   ---00  2.4300  2.5300    2.63  2.4510     2.58  ---00
4 NaN  2.3593  2.4100  2.4900  2.6300  2.4910     2.59  ---00
5 NaN   ---00  2.1593  2.4295   ---00  2.2010   2.4500  ---00
6 NaN  2.0400  2.1493  2.2495   ---00    2.05    ---00   2.24]

运行 没有 2

[    0       1       2       3       4       5        6      7
0 NaN   ---00  2.3593  2.4800    2.58   ---00     2.53  ---00
1 NaN   ---00  2.4300  2.5300    2.63  2.4510     2.58  ---00
2 NaN   ---00  2.1593  2.4295   ---00  2.2010   2.4500  ---00
3 NaN  2.3593  2.4100  2.4900  2.6300  2.4910     2.59  ---00
4 NaN  2.0400  2.1493  2.2495   ---00    2.05    ---00   2.24
5 NaN   ---00  2.4992  2.5700    2.64    2.63  2.59100  ---00
6 NaN   ---00  2.0493  2.2495   ---00  2.0500   2.2400  ---00]

理想情况下,根据您的代码,每次 运行 您的代码(在问题中给出)时,您应该得到 2.41 的结果。

实际情况是此页面在后台进行了一些 javascript 授权,然后才填充有效数据。

对于这些类型的页面,最好使用 selenium

from selenium import webdriver
from time import sleep
from bs4 import BeautifulSoup
driver = webdriver.Firefox()
driver.get('https://taripebi.ge/%E1%83%91%E1%83%94%E1%83%9C%E1%83%96%E1%83%98%E1%83%9C%E1%83%98%E1%83%A1-%E1%83%A4%E1%83%90%E1%83%A1%E1%83%94%E1%83%91%E1%83%98')
source = driver.page_source
soup =BeautifulSoup(source, 'lxml')
for paragraph in soup.find('div', style = "width: 40%;/* float: left; */"):
    print(paragraph)

输出

运行 没有 1

2.41

运行 没有 2

2.41