来自 BeautifulSoup4 Webscrape 的时区与原始来源不同
Timezone from BeautifulSoup4 Webscrape is different from original source
我正在使用 beautifulsoup4 从 google 日历中抓取信息。我生成了一个包含日期的列表,后面是预约人员的姓名,以及会议举行的时间。然而,由于某种原因,网络抓取生成的时间提前了 5 个小时,我不知道为什么。
这是我用来生成列表的内容:
import requests
import re
from bs4 import BeautifulSoup
url = "https://calendar.google.com/calendar/htmlembed?src=stationhouston.com_rjtfsabha07jarsumdg7v95b10@group.calendar.google.com&&mode=AGENDA"
r = requests.get(url)
soup = BeautifulSoup(r.content)
soup2 = soup.find_all("div", {"class":"date-section"})
for item in soup2:
print item.text
原始来源以 CST 显示时间,而网络抓取以 UTC 生成时间。
在抓取 url 之前必须更改时区吗?或者有没有办法使用 python?
来解决这个问题
Google 日历没有获取任何时区信息,因为您没有使用浏览器。代替特定时区,它将始终默认为 UTC。
所以,有点痛苦,但你可以这样做:
from datetime import datetime
from dateutil import tz
import requests
import re
from bs4 import BeautifulSoup
from dateutil.parser import parse
def convert_time(x):
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')
utc = x.replace(tzinfo=from_zone)
central = x.astimezone(to_zone)
return central
url = "https://calendar.google.com/calendar/htmlembed?src=stationhouston.com_rjtfsabha07jarsumdg7v95b10@group.calendar.google.com&&mode=AGENDA"
r = requests.get(url)
soup = BeautifulSoup(r.content)
soup2 = soup.find_all("div", {"class":"date-section"})
for item in soup2:
try:
time_str = re.search('[0-9]:[0-9][0-9]', item.text).group(0)
print("Old time was: {}".format(time_str))
time_parsed = parse(time_str)
res = convert_time(time_parsed)
new_time = '{}:{}'.format(res.hour, res.minute)
print("New time is: {}".format(new_time))
except:
pass
这里我们使用正则表达式从字符串中提取时间。
我们可以使用 datetime.parser
工具自动将该字符串转换为 Python datetime
对象。
从那里我们使用上面定义的 convert_time()
函数将该 UTC 时间戳转换为 CST 时间戳。
如您所见,输出似乎是正确的:
Old time was: 2:30
New time is: 22:30
Old time was: 2:30
New time is: 22:30
Old time was: 6:30
New time is: 2:30
Old time was: 3:30
New time is: 23:30
Old time was: 4:30
New time is: 0:30
Old time was: 7:30
我正在使用 beautifulsoup4 从 google 日历中抓取信息。我生成了一个包含日期的列表,后面是预约人员的姓名,以及会议举行的时间。然而,由于某种原因,网络抓取生成的时间提前了 5 个小时,我不知道为什么。
这是我用来生成列表的内容:
import requests
import re
from bs4 import BeautifulSoup
url = "https://calendar.google.com/calendar/htmlembed?src=stationhouston.com_rjtfsabha07jarsumdg7v95b10@group.calendar.google.com&&mode=AGENDA"
r = requests.get(url)
soup = BeautifulSoup(r.content)
soup2 = soup.find_all("div", {"class":"date-section"})
for item in soup2:
print item.text
原始来源以 CST 显示时间,而网络抓取以 UTC 生成时间。
在抓取 url 之前必须更改时区吗?或者有没有办法使用 python?
来解决这个问题Google 日历没有获取任何时区信息,因为您没有使用浏览器。代替特定时区,它将始终默认为 UTC。
所以,有点痛苦,但你可以这样做:
from datetime import datetime
from dateutil import tz
import requests
import re
from bs4 import BeautifulSoup
from dateutil.parser import parse
def convert_time(x):
from_zone = tz.gettz('UTC')
to_zone = tz.gettz('America/New_York')
utc = x.replace(tzinfo=from_zone)
central = x.astimezone(to_zone)
return central
url = "https://calendar.google.com/calendar/htmlembed?src=stationhouston.com_rjtfsabha07jarsumdg7v95b10@group.calendar.google.com&&mode=AGENDA"
r = requests.get(url)
soup = BeautifulSoup(r.content)
soup2 = soup.find_all("div", {"class":"date-section"})
for item in soup2:
try:
time_str = re.search('[0-9]:[0-9][0-9]', item.text).group(0)
print("Old time was: {}".format(time_str))
time_parsed = parse(time_str)
res = convert_time(time_parsed)
new_time = '{}:{}'.format(res.hour, res.minute)
print("New time is: {}".format(new_time))
except:
pass
这里我们使用正则表达式从字符串中提取时间。
我们可以使用 datetime.parser
工具自动将该字符串转换为 Python datetime
对象。
从那里我们使用上面定义的 convert_time()
函数将该 UTC 时间戳转换为 CST 时间戳。
如您所见,输出似乎是正确的:
Old time was: 2:30
New time is: 22:30
Old time was: 2:30
New time is: 22:30
Old time was: 6:30
New time is: 2:30
Old time was: 3:30
New time is: 23:30
Old time was: 4:30
New time is: 0:30
Old time was: 7:30