微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单击CSV文件中列出的多个页面的单击按钮后,如何抓取表格?硒,Python

如何解决单击CSV文件中列出的多个页面的单击按钮后,如何抓取表格?硒,Python

我想使用pd.read_html函数将表示多个URL的表中的所有信息抓取。网站的示例为:https://www.top40.nl/10cc/10cc-donna-5867,我通过csv文件导入。

进入网站并单击选项卡“ Songinfo”后,带有所有相关信息的表将变为可见。请在下面找到我的代码。 Python给出错误:未找到表和/或无法从列表中解析。很高兴听到有关如何更正我的代码的任何建议:

df_list = []

with open(r"C:\Users\nlvijn02\Documents\Personal documents\Sony\Test_input_links.csv") as file:    
    reader = csv.reader(file)
    for row in reader:
        print(row[0])
        driver.get(row[0])
                
        driver.find_element_by_xpath("//a[@href='#songinfo']").click()
        
        table = driver.find_elements_by_xpath("""//*[@id="songinfo"]/table""")
    
        df_list.append(pd.read_html(table))
            
    df = pd.concat(df_list)
        
driver.close()        
df.to_csv("details.csv")

请在表格的HTML代码下面找到:

<div id="songinfo" class="tab-pane active" aria-expanded="true"><h2>Songinformatie</h2><table class="table-songinfo"><tbody><tr><th>Artiest</th><td><a data-linktype="artist" href="https://www.top40.nl/top40-artiesten/10cc">10cc</a></td></tr><tr><th>&nbsp;</th><th style="text-align: left;">A-kant</th></tr><tr><th>Titel</th><td>
                                                                                                            Donna                                                                                                   </td></tr><tr><th>Lengte</th><td>
                                                                                                            02:55
                                                                                                    </td></tr><tr><th>Componist(en)</th><td>
                                                                                                            Kevin Godley,Lol Creme
                                                                                                    </td></tr><tr><th>&nbsp;</th><th style="text-align: left;">B-kant</th></tr><tr><th>Titel</th><td>
                                                                                                            Hot Sun Rock
                                                                                                    </td></tr><tr><th>Lengte</th><td>
                                                                                                            03:00
                                                                                                    </td></tr><tr><th>Componist(en)</th><td>
                                                                                                            Eric Stewart,Graham Gouldman
                                                                                                    </td></tr><tr><th colspan="2">&nbsp;</th></tr><tr><th>Platenlabel</th><td>
                                                                                                    UK
                                                                                            </td></tr><tr><th>Catalogusnr</th><td>
                                                                                                    UK 6
                                                                                            </td></tr><tr><th>Hoogste positie UK</th><td>
                                                                                                    2
                                                                                            </td></tr></tbody></table></div>

解决方法

df_list = []

with open(r"C:\Users\nlvijn02\Documents\Personal documents\Sony\Test_input_links.csv") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row[0])
        driver.get(row[0])

        driver.find_element_by_xpath("//a[@href='#songinfo']").click()

        table = driver.find_element_by_xpath("""//*[@id="songinfo"]/table""")

        df_list.append(pd.read_html(table.get_attribute('outerHTML')))

    df = pd.concat(df_list)

driver.close()
df.to_csv("details.csv")

我在您的代码中修改了2行。

  1. find_elements_by_xpath => find_element_by_xpath
  2. table => table.get_attribute('outerHTML')

如果您测试我的代码并让我知道结果,我将非常高兴。 最好的问候

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。