【问题标题】:I wrote a small web scrape script and i need help automating it我写了一个小的网络抓取脚本,我需要帮助自动化它
【发布时间】:2020-11-11 00:12:10
【问题描述】:

我已经学习 Python 几个星期了,我想编写一个脚本来自动完成一项我必须为我的工作完成的非常繁琐的任务,我基本上必须浏览我所在城市的 2000 多个慈善机构的目录并获取一些信息并从中制作 Excel 表格。 Here's what i've got so far

'Book3.xlsx' 包含我用不同脚本抓取的 URL 列表。上面的脚本访问链接,抓取我想要的信息并将其保存在“Book4.xlsx”中。但正如你所见,是否可以一次只做一个。

我一直在努力使其自动化。已经尝试(以我非常有限的知识)循环,定义上述脚本的函数并循环它等,但目前还没有雪茄。

任何帮助、提示等将不胜感激。谢谢!

【问题讨论】:

    标签: python loops web-scraping beautifulsoup automation


    【解决方案1】:

    您是否考虑过使用 .cell() 方法?我不是 openpyxl 专家,但这可以使用 for 循环:

    #
    # You load your sheets
    #
    
    # You make your 'for' loop as long as you want in order to go over every URL in 'Book3.xslx'
    for i in range(2, 11):
        source = requests.get(sheet.cell(row=i, column=1).value).text
    
        #
        # You select the info you want
        #
    
        outputsheet.cell(row=i, column=1) = name
        outputsheet.cell(row=i, column=2) = mobile + " " + phone
        outputsheet.cell(row=i, column=3) = email
        outputsheet.cell(row=i, column=4) = website
    
    outputwb.save("Book4.xslx")
    

    编辑:您必须将所有 URL 存储在“Book3.xslx”(A 列)的第一列中,并且对于每个 URL,输出将写入不同的行(1、2、3、等等。)。在这个特定的示例中,只有 8 个 URL(从第 2 行到第 10 行(包括 10 个))。

    【讨论】:

    • 谢谢!当我使用 .cell() 方法时,for 循环起作用了。虽然我认为我尝试将其保存到新的 xslx 工作表而不是同一个工作表,这让我自己变得更难了。稍作调整,下面的代码对输出很有帮助。 name_cell = sheet.cell(row, 2) tele_cell = sheet.cell(row, 3) email_cell = sheet.cell(row, 4) web_cell = sheet.cell(row, 5) name_cell.value = name tele_cell.value = mobile + ' ' + 电话 email_cell.value = 电子邮件 web_cell.value = 网站
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多