使用Python从多个网页中提取日期

背后

我想提取新闻文章在网站上发布的日期。对于某些网站,我具有确切的html元素,其中日期/时间为(div,p,时间),但是在某些网站上,我没有:

这些是某些网站(德语网站)的链接:

(2020年11月3日)http://www.linden.ch/de/aktuelles/aktuellesinformationen/?action=showinfo&info_id=1074226

(2020年12月1日)http://www.reutigen.ch/de/aktuelles/aktuellesinformationen/welcome.php?action=showinfo&info_id=1066837&ls=0&sq=&kategorie_id=&date_from=&date_to=

(2020年10月22日)http://buchholterberg.ch/de/Gemeinde/Information/News/Newsmeldung?filterCategory=22&newsid=905

我已经试过3个不同的解决方案,与Python库等requestshtmldate以及date_guesser但是我一直都想与无,或在情况下htmldate的lib,我总是得到相同的日期(2020年1月1日)

from bs4 import BeautifulSoup
import requests
from htmldate import find_date
from date_guesser import guess_date, Accuracy

# Lib find_date
url = "http://www.linden.ch/de/aktuelles/aktuellesinformationen/?action=showinfo&info_id=1074226"
response = requests.get(url)
my_date = find_date(response.content, extensive_search=True)
print(my_date, '\n')


# Lib guess_date
url = "http://www.linden.ch/de/aktuelles/aktuellesinformationen/?action=showinfo&info_id=1074226"
my_date = guess_date(url=url, html=requests.get(url).text)
print(my_date.date, '\n')


# Lib Requests # I DO NOT GET last modified TAG
my_date = requests.head('http://www.linden.ch/de/aktuelles/aktuellesinformationen/?action=showinfo&info_id=1074226')
print(my_date.headers, '\n')

难道我做错了什么?

能否请您告诉我,有没有一种方法可以从这样的网站(我没有特定的div,p和datetime元素)中提取发布日期。

重要!我想进行通用日期提取,以便可以将这些链接放入for循环并对其运行相同的功能。

生活很复杂

对于某些日期解析库,我从来没有取得太大的成功,所以我通常会走另一条路。我相信从您所质疑的这些网站中提取日期字符串的最佳方法是使用正则表达式。

网址:linden.ch

import requests
import re as regex
from bs4 import BeautifulSoup
from datetime import datetime

url = "http://www.linden.ch/de/aktuelles/aktuellesinformationen/?action=showinfo&info_id=1074226"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
page_body = soup.find('body')
find_date = regex.search(r'(Datum der Neuigkeit)\s(\d{1,2}\W\s\w+\W\s\d{4})', str(page_body))
reformatted_timestamp = datetime.strptime(find_date.groups()[1], '%d. %b. %Y').strftime('%d-%m-%Y')
print(reformatted_timestamp)
# print output 
03-11-2020

网站:buchholterberg.ch

import requests
import re as regex
from bs4 import BeautifulSoup
from datetime import datetime

url = "http://buchholterberg.ch/de/Gemeinde/Information/News/Newsmeldung?filterCategory=22&newsid=905"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
page_body = soup.find('body')
find_date = regex.search(r'(Veröffentlicht)\s\w+:\s(\d{1,2}:\d{1,2}:\d{1,2})\s(\d{1,2}.\d{1,2}.\d{4})', str(page_body))
reformatted_timestamp = datetime.strptime(find_date.groups()[2], '%d.%m.%Y').strftime('%d-%m-%Y')
print(reformatted_timestamp)
# print output
22-10-2020

更新12-04-2020

我查看了您提到的两个Python库的源代码:htmldate和date_guesser。这些库目前都无法从问题中列出的3个来源中提取日期。缺乏提取的主要原因与这些目标站点的日期格式和语言(德语)有关。

我有一些空闲时间,所以我为您准备了这些时间。下面的答案可以很容易地修改以从任何网站中提取,并可以根据目标源的格式根据需要进行优化。当前,它从URL中包含的所有链接中提取。


所有网址

import requests
import re as regex
from bs4 import BeautifulSoup

def extract_date(can_of_soup):
   page_body = can_of_soup.find('body')
   clean_body = ''.join(str(page_body).replace('\n', ''))
   if 'Datum der Neuigkeit' in clean_body or 'Veröffentlicht' in clean_body:
     date_formats = '(Datum der Neuigkeit)\s(\d{1,2}\W\s\w+\W\s\d{4})|(Veröffentlicht am: \d{2}:\d{2}:\d{2} )(\d{1,2}.\d{1,2}.\d{4})'
     find_date = regex.search(date_formats, clean_body, regex.IGNORECASE)
     if find_date:
        clean_tuples = [i for i in list(find_date.groups()) if i]
        return ''.join(clean_tuples[1])
   else:
       tags = ['extra', 'elementStandard elementText', 'icms-block icms-information-date icms-text-gemeinde-color']
       for tag in tags:
          date_tag = page_body.find('div', {'class': f'{tag}'})
          if date_tag is not None:
            children = date_tag.findChildren()
            if children:
                find_date = regex.search(r'(\d{1,2}.\d{1,2}.\d{4})', str(children))
                return ''.join(find_date.groups())
            else:
                return ''.join(date_tag.contents)


def get_soup(target_url):
   response = requests.get(target_url)
   soup = BeautifulSoup(response.content, 'html.parser')
   return soup


urls = {'http://www.linden.ch/de/aktuelles/aktuellesinformationen/?action=showinfo&info_id=1074226',
    'http://www.reutigen.ch/de/aktuelles/aktuellesinformationen/welcome.php?action=showinfo&info_id=1066837&ls=0'
    '&sq=&kategorie_id=&date_from=&date_to=',
    'http://buchholterberg.ch/de/Gemeinde/Information/News/Newsmeldung?filterCategory=22&newsid=905',
    'https://www.steffisburg.ch/de/aktuelles/meldungen/Hochwasserschutz-und-Laengsvernetzung-Zulg.php',
    'https://www.wallisellen.ch/aktuellesinformationen/924227',
    'http://www.winkel.ch/de/aktuellesre/aktuelles/aktuellesinformationen/welcome.php?action=showinfo&info_id'
    '=1093910&ls=0&sq=&kategorie_id=&date_from=&date_to=',
    'https://www.aeschi.ch/de/aktuelles/mitteilungen/artikel/?tx_news_pi1%5Bnews%5D=87&tx_news_pi1%5Bcontroller%5D=News&tx_news_pi1%5Baction%5D=detail&cHash=ab4d329e2f1529d6e3343094b416baed'}


for url in urls:
   html = get_soup(url)
   article_date = extract_date(html)
   print(article_date)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用python scrapy从网页中提取链接

来自分类Dev

使用Python从不断更新的网页中提取内容

来自分类Dev

如何通过Python使用Selenium从网页中提取文本$ 7.56

来自分类Dev

使用 Python 抓取网页时从链接中提取 href

来自分类Dev

尝试使用HtmlAgilityPack从网页中提取数据

来自分类Dev

使用Jsoup从网页中提取语言

来自分类Dev

使用 VBA 从网页表格中提取数据

来自分类Dev

使用 R 从网页中提取中间名

来自分类Dev

使用BS4从网页中提取多个没有'a'或'href'标签的URL

来自分类Dev

如何使用Jsoup从网页中提取多个电子邮件地址?

来自分类Dev

从多个日期范围中提取每周的天数

来自分类Dev

strptime()错误,使用python从时间戳中提取日期

来自分类Dev

Python:阅读网页并从该网页中提取文本

来自分类Dev

从网页[R]中提取所有(可能的)可选日期值

来自分类Dev

使用SQL Server从具有多个日期的字符串中提取最小日期

来自分类Dev

如何使用Python从多个文件中提取数据?

来自分类Dev

Python使用xarray从NETCDF文件中提取多个纬度/经度

来自分类Dev

使用Python从字典中提取多个数据框

来自分类Dev

使用Python从简历中提取多个字段

来自分类Dev

使用flask和python从mongo中提取多个记录

来自分类Dev

使用python从多个文本文件中提取数据

来自分类Dev

如何从多个网页中提取链接并删除重复项

来自分类Dev

如何在python 3中使用Selenium phantomJS从网页的html源中提取表格元素?

来自分类Dev

我需要使用Python / BeautifulSoup从网页中提取嵌入式.xlsx链接的帮助

来自分类Dev

如何使用Python从包含“显示更多”的网页中提取所有URL?

来自分类Dev

如何从包含日期的多个文件中提取日期?

来自分类Dev

使用dplyr从日期中提取月份

来自分类Dev

使用熊猫从日期列中提取年份

来自分类Dev

从网页抓取的HTML页面的Python脚本中提取列表

Related 相关文章

  1. 1

    使用python scrapy从网页中提取链接

  2. 2

    使用Python从不断更新的网页中提取内容

  3. 3

    如何通过Python使用Selenium从网页中提取文本$ 7.56

  4. 4

    使用 Python 抓取网页时从链接中提取 href

  5. 5

    尝试使用HtmlAgilityPack从网页中提取数据

  6. 6

    使用Jsoup从网页中提取语言

  7. 7

    使用 VBA 从网页表格中提取数据

  8. 8

    使用 R 从网页中提取中间名

  9. 9

    使用BS4从网页中提取多个没有'a'或'href'标签的URL

  10. 10

    如何使用Jsoup从网页中提取多个电子邮件地址?

  11. 11

    从多个日期范围中提取每周的天数

  12. 12

    strptime()错误,使用python从时间戳中提取日期

  13. 13

    Python:阅读网页并从该网页中提取文本

  14. 14

    从网页[R]中提取所有(可能的)可选日期值

  15. 15

    使用SQL Server从具有多个日期的字符串中提取最小日期

  16. 16

    如何使用Python从多个文件中提取数据?

  17. 17

    Python使用xarray从NETCDF文件中提取多个纬度/经度

  18. 18

    使用Python从字典中提取多个数据框

  19. 19

    使用Python从简历中提取多个字段

  20. 20

    使用flask和python从mongo中提取多个记录

  21. 21

    使用python从多个文本文件中提取数据

  22. 22

    如何从多个网页中提取链接并删除重复项

  23. 23

    如何在python 3中使用Selenium phantomJS从网页的html源中提取表格元素?

  24. 24

    我需要使用Python / BeautifulSoup从网页中提取嵌入式.xlsx链接的帮助

  25. 25

    如何使用Python从包含“显示更多”的网页中提取所有URL?

  26. 26

    如何从包含日期的多个文件中提取日期?

  27. 27

    使用dplyr从日期中提取月份

  28. 28

    使用熊猫从日期列中提取年份

  29. 29

    从网页抓取的HTML页面的Python脚本中提取列表

热门标签

归档