簡體   English   中英

如何使用bs4從網站中提取標簽下的數據

[英]how to extract data under a tag from website using bs4

<html>

<head>
  <title>Index of /pub/opera/desktop/</title>
</head>

<body>
  <h1>Index of /pub/opera/desktop/</h1>
  <hr>
  <pre><a href="../">../</a>
<a href="15.0.1147.130/">15.0.1147.130/</a>                                     01-Jul-2013 15:18                   -
<a href="15.0.1147.132/">15.0.1147.132/</a>                                     01-Jul-2013 15:18                   -
<a href="15.0.1147.138/">15.0.1147.138/</a>                                     09-Jul-2013 12:11

我需要提取版本 15.0.1147.130 和日期 01-Jul-2013 15:18 但是,使用我的代碼,它只給我版本

soup = BeautifulSoup(requests.get('https://get.geo.opera.com/pub/opera/desktop/').text, 'html.parser')
for item in soup.find('pre').find_all('a')[1:]:
    print(item)

我還缺少什么來獲取日期文本?

你得到“A”標簽,它們不包含日期

    soup = BeautifulSoup(requests.get('https://get.geo.opera.com/pub/opera/desktop/').text, 'html.parser')
    for item in soup.find_all('pre'):
    version = item
    print(version.getText().replace('/', "").replace('-', ""))

更新

import requests
from bs4 import BeautifulSoup
import re


soup = BeautifulSoup(requests.get('https://get.geo.opera.com/pub/opera/desktop/').text, 'html.parser')
lines = soup.find('pre').getText().replace('/', "").replace('-', "").split('\r')

for line in lines[1:-1]:
    my_data = re.sub(' +', ' ', line).split(' ')
    geo = my_data[0]
    date = my_data[1]
    time = my_data[2]
    print(geo, date, time)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM