【问题标题】:Extracting data from a calendar with Python and Beautifulsoup使用 Python 和 Beautifulsoup 从日历中提取数据
【发布时间】:2018-07-04 18:19:29
【问题描述】:

我想获取日历中的数据:

http://www.purebhakti.com/component/panjika

我考虑过使用 Python 和 beautifulsoap,但我接受了建议。

我想参加当天的活动:

2017 年 4 月 22 日:Ekādaśī,K,06:09,Śatabhiṣā

+ŚUDDHA EKĀDAŚĪ VRATA:Varūthinī EKADASI 的斋戒

如何让程序到达日历(自动选择时区和城市后)?例如: 时区 = -3:00 布宜诺斯艾利斯 城市 = 里约热内卢

from bs4 import BeautifulSoup
import requests

url = 'http://www.purebhakti.com/component/panjika'
header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                        'AppleWebKit/537.36 (KHTML, like Gecko) '
                        'Chrome/51.0.2704.103 Safari/537.36'}



req = requests.get(url,headers= header)

html = req.text

soup = BeautifulSoup(html,'html.parser')

【问题讨论】:

  • 你有什么尝试吗?到目前为止你的努力是什么?
  • 我不知道在网页中找到我要选择的元素!

标签: python web-scraping beautifulsoup


【解决方案1】:

有很多方法可以解决这个问题:

-您可以使用 Selenium WebDriver 单击按钮并选择时区和城市。

-Selenium 之外的另一个选择是使用 pyautogui(“pyautogui.locateOnScreen”函数)。

-下载日历(网页源)使用urllib2

-要从日历中获取必要的数据,请使用 Beautiful Soap

【讨论】:

  • 他正在使用python 3。在python 3中没有名为urllib2的模块。
  • @KhairulBasarRofi 你是对的。有:urllib.request 模块。
【解决方案2】:
import requests, bs4
from urllib.parse import parse_qsl

qs = 'action=2&timezone=23&location=Rio+de+Janeiro%2C+Brazil++++++++043W15+22S54+++++-3.00&button=Get+Calendar'
payload = dict(parse_qsl(qs))
r = requests.post('http://www.purebhakti.com/component/panjika', data=payload)

当您点击按钮时,您正在向服务器发布数据,您可以在chrome开发工具中找到数据。

我们可以通过requests.post()模仿这种行为

我还使用parse_qsl 将编码的 url 转换为 python dict:

{'action': '2',
'button': 'Get Calendar',
 'location': 'Rio de Janeiro, Brazil        043W15 22S54     -3.00',
 'timezone': '23'}

【讨论】:

  • 谢谢。我试图理解代码。什么是“日期”?数据'未定义。请帮帮我好吗?
  • @Ed S 当你使用 post 时,你正在向服务器发送数据,它是一个包含 k-v 对的字典。docs.python-requests.org/en/master/user/quickstart/…
  • payload = dict(parse_qsl(data)) NameError: name 'data' is not defined
  • @Ed S 我的错,我更新了代码并随时接受我的回答
  • 我是编程新手。很抱歉给您带来不便。
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多