用python(靓汤)刮网点击问题

2024-04-29 12:19:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的,很乐意得到帮助。我试图从网站https://www.boerse.de/indizes/Dax/DE0008469008收集数据。在这个网站上,Dax30公司列在下面。我想从2000年3月到2019年收集每家公司的所有股票价格。因此我需要点击公司名称。在下一页有一个按钮“历史”。在该页面加载之后,出现了股票价格页面。有一份每月股票价格的清单。这是第二张桌子,名为“阿迪达斯莫纳茨施卢斯库斯2019”。当我在三月“März”点击这个时,一个新的表格“Adidas Monats Schlusskurse”出现了,上面有每年三月的所有股票价格。我正在使用python和beautifulsoup。我的问题是,由于onclick html语法,我无法访问“Adidas Monats Schlusskurse”表。我设法找到了正确的href=#但它不是一个链接,所以我无法找到我想要的表。下面是我的代码:

import bs4
from urllib.request import urlopen as ureq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.boerse.de/indizes/Dax/DE0008469008'
uclient = ureq(my_url)
page_html = uclient.read()
uclient.close()

page_soup = soup(page_html, "html.parser")

aktien = page_soup.findAll("a",{"class":"nobr"})

for aktie in aktien:
    name = aktie.text                                                   #getting the name of the company

    historie = aktie["href"]                                            #getting the link and then open next website
    hclient = ureq(historie)
    h_html = hclient.read()
    hclient.close()
    hpage_soup = soup(h_html, "html.parser")

    block = hpage_soup.find("div",{"class":"col-sm-2 navColumn"})
    link = "https://www.boerse.de" + block.findAll("a")[1].get("href")  #getting the link Historie

    kclient = ureq(link)
    k_html = kclient.read()
    kclient.close()
    kpage_soup = soup(k_html, "html.parser")                            #open website with stock prices

    kurs = kpage_soup.find("div", {"id": "histKurseMonth"})
    button = kurs.findAll("a")[2].get("href")                           #getting the link for the website with stock prices for march
                                                                        #problem href="#" onclick="historieMonatJahr('DE0007472060', '3', '');return false;"
                                                                        #what do I need to do to get to the stock prices for march?
                                                                        #Don't know what to do with onclick

我感谢你的帮助。你知道吗

编辑:

多亏了Andrej Kesely,我得到了我想要的东西,但是有没有人能理解我的代码并告诉我如何使用我的代码呢?我只需要帮助就可以通过点击页面。所以,如果有人只添加代码就可以参加游行,那就太好了,因为我想自己学习编程,而不是交付解决方案。你知道吗


Tags: theto代码httpsforhtmlwwwpage