【问题标题】:How to give inputs to a website using python如何使用 python 向网站提供输入
【发布时间】:2015-02-28 02:45:54
【问题描述】:

大家好,我是 python 新手。请帮我解决这个要求。

http://www.example.com/ratings/ratings-rationales.jsp?date=true&result=true

在此链接中,我必须先选择日期,然后评级公司会将其出版物列为链接。现在我想搜索一个包含我感兴趣的单词的链接说“稳定”。我使用 python 3.4.2 尝试了以下操作

from bs4 import BeautifulSoup
from urllib.parse import urljoin
import requests

url = "http://www.example.com/ratings/ratings-rationales.jsp?date=true&result=true"   
r = requests.get(url)
soup = BeautifulSoup(r.content)

example_links = lambda tag: getattr(tag, 'name', None) == 'a' and 'stable' in tag.get_text().lower() and 'href' in tag.attrs
results = soup.find_all(example_links)
result_links = [urljoin(url, tag['href']) for tag in results]
print (result_links)

这不是打印任何东西。我在下面看到结果

>>>
[]

显然我没有提供日期作为输入。
1.今天的日期如何输入起止日期? (显然要定期检查包含感兴趣单词的链接的更新,这将是以后的问题)
例如,在从日期:31-12-2014 到日期:31-12-2014 作为输入之后

是我需要的超链接输出。

任何建议都会非常有用。提前致谢

这是更新后的代码,我仍然无法得到结果。 >>> [] 是输出

from datetime import datetime
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import requests

#Getting the current date
today = datetime.today()

#For the sake of brevity some parameters are missing on the payload
payload = {
    'selArchive': 1,
    'selDay': 31, 
    'selMonth': 12, 
    'selYear': 2014,
    'selDay1': 31, 
    'selMonth1': 12, 
    'selYear1': 2014,
    'selSector': '',
    'selIndustry': '',
    'selCompany': ''
}

example_url = "http://www.example.com/
r = requests.post(example_url, data=payload)    
rg = requests.get(example_url)
soup = BeautifulSoup(rg.content)

crisil_links = lambda tag: getattr(tag, 'name', None) == 'a' and 'stable' in tag.get_text().lower() and 'href' in tag.attrs   
results = soup.find_all(example_links)
result_links = [urljoin(url, tag['href']) for tag in results]
print (result_links)

【问题讨论】:

  • 您应该考虑到日期不能相等,并且它们的差异不能超过一个月。
  • 但在网站上,我给出了相同的结果(2014 年 12 月 31 日)。我只看到这个条件和另外两个条件,但不是相同的日期条件if(todate-fromdate>2678400000){ alert('The Date range can not exceeds one month'); document.frmCrisil.selDay.focus(); return false; }
  • 是的,但是当您尝试单击具有相同日期的日期范围时,会显示错误消息。
  • 我不确定我是否理解。如果您尝试输入今天的日期,它将不会显示任何内容,因为今天还没有更新。但如果您有任何日期(星期日除外),您可以看到结果。再次抱歉,如果我占用了您的时间。所以我认为同一日期是有效的。不是吗?
  • 是的,你是对的......我正在尝试未来的约会

标签: python html python-3.x beautifulsoup html-parsing


【解决方案1】:

您应该为此特定站点执行 POST 而不是 GET(this link 了解如何使用参数形成发布请求)。

检查这个例子:

from datetime import datetime
from urllib.parse import urljoin

from bs4 import BeautifulSoup

import requests

#Getting the current date
today = datetime.today()

#Here I'm only passing from and to dates (current date) and the industry parameter
payload = {
    'selDay': 31, 
    'selMonth': 12, 
    'selYear': 2014,
    'selDay1': 31, 
    'selMonth1': 12, 
    'selYear1': 2014,
    'selIndustry': '',
    'txtPhrase': '',
    'txtInclude': '',
    'txtExclude': '',
    'selSubServices': 'ALL',
    'selServices': 'all',
    'maxresults': 10,
    'pageno': 1,
    'srchInSrchCol': '01',
    'sortOptions': 'date',
    'isSrchInSrch': '01',
    'txtShowQuery': '01',
    'tSearch': 'Find a Rating',
    'txtSearch': '',
    'selArchive': 1,
    'selSector': 148,
    'selCompany': '',
    'x': 40,
    'y': 11,
}

crisil_url = "http://www.crisil.com/ratings/ratings-rationales.jsp?result=true&Sector=true"
r = requests.post(crisil_url, data=payload)

soup = BeautifulSoup(r.content)

crisil_links = lambda tag: getattr(tag, 'name', None) == 'a' and 'stable' in tag.get_text().lower() and 'href' in tag.attrs
results = soup.find_all(crisil_links)
result_links = [urljoin(crisil_url, tag['href']) for tag in results]
print (result_links)

您需要检查您正在过滤的行业的 ID,因此请务必通过 Inspect Element 检查它们,在浏览器上选择行业选择框。

之后,您将获得响应并通过 BeautifulSoup 进行解析,就像您现在所做的那样。

定期检查: 要定期检查这一点,如果使用 Linux/Unix,则应考虑使用 crontab;如果使用 Windows,则应考虑计划任务。

【讨论】:

  • 也许添加一个发布请求的实际示例而不是链接会更好
  • @avenet 您能否建议如何从我们将输入传递到的网站获得响应。以下是我使用的方式。但没有任何结果r = requests.post(crisil_url, data=payload) rg = requests.get(crisil_url) soup = BeautifulSoup(rg.content)
  • 结果如何?我可以从请求中得到结果,但它是空的结果列表,因为我发送给您的列表中的某些参数丢失了,因此您需要全部提供
  • 顺便说一句,此站点需要很长时间(5 或 6 秒)来处理您的 POST 请求,所以您应该考虑这一点!!
  • 是的,有点慢。您能否指导我如何找到我需要为其提供值的其他参数,或者您发送的任何内容都是完整的。我找到了这个部分。请原谅我不知道 html var day = document.frmCrisil.selDay.value; var month = document.frmCrisil.selMonth.value; month = month-1; var year = document.frmCrisil.selYear.value; var day1 = document.frmCrisil.selDay1.value; var month1 = document.frmCrisil.selMonth1.value; month1 = month1-1; var year1 = document.frmCrisil.selYear1.value;
猜你喜欢
  • 2015-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 2011-10-31
  • 2018-12-10
  • 1970-01-01
相关资源
最近更新 更多