【问题标题】:Need help some with simple python find in strings需要一些简单的python在字符串中查找的帮助
【发布时间】:2020-05-31 17:58:52
【问题描述】:

我是新手编码员,我需要一些有关 xxx.find 用法的帮助...

总结

我正在使用 selenium 进行网络自动化项目。 这个python项目的主要目标是

第 1 步。使用提供的用户名和密码自动登录我学校的 LMS 网站

第 2 步。获取我的学校托管所有作业 PDF 文件的给定网站 URL 的 page_source

第 3 步。对 page_source 字符串上的 PDF viewer 链接进行匹配搜索

第 4 步。导航到匹配的 PDF viewer URL 并再次获取 page_source

第 5 步。搜索var DEFAULT_URL = '/icom/files/b729266c557f5f7108894ade1668d55a.pdf'或相关链接,这是我的学校服务器托管pdf文件的链接

第 6 步。获取值/icom/files/xxxxxx.pdf 并与www.icom.org.cn/ 之类的标头组合,使其变为www.icom.org.cn/icom/files/xxxx.pdf

第 7 步。然后执行wget 命令或其他东西来获取pdf文件。

现在的问题

我以某种方式正确获得了登录自动化,但现在卡在 page_source 匹配过程中,但不知何故卡在 第 3 步

进入我的学校托管所有 PDF 作业的网站 URL 后。

我跑了一个sauce = driver.pages_source,我正在尝试匹配它

word = ("/icom/faculty/viewer/?id")
print(sauce.find(word))

但是查找的输出是5122

我希望 str.find 的用法能够匹配并列出所有匹配结果,并以行而不是数字输出值...

那么……我该怎么做呢?我在互联网上搜索了这个,但对我来说仍然没有太多信息。对不起,我只是编程的初学者,我的英语不太好。提前谢谢你。

顺便说一句,我脑子里突然冒出一个问题。

如果我能够匹配 第 3 步中的值。如何对其进行编码以在 PDF 查看器链接 中包含额外的值?

因为完整的链接会是这样的:

/icom/faculty/viewer/?id=1260&type=2英文作业

/icom/faculty/viewer/?id=1254&type=2数学作业

/icom/faculty/viewer/?id=775&type=2 用于作业封面等

额外的值id=xxxx 总是不同的。那么...我如何对其进行编码以匹配并列出所有匹配结果+额外值

任何帮助将不胜感激!非常感谢!

page_source 的确切示例

  </tr>
          <tr>
                <td valign="top" width="100"><a href="javascript:void(0)" onclick="return popup2('/icom/student/main/report?varCrs_ID=FP102-001&varSemester_code=1/20&prog_id=FIM')">FP102-001</a></td>
        <td valign="top" width="150">MUSIC PERFORMANCE LAB </td>
        <td valign="top" width="100" align="center">IBS<br>(<a href="mailto:abcder@icom.edu.my">fafafan@icom.edu.my</a>)        </td>
        <td valign="top" width="100" align="center">3 </td>
                <td valign="top" width="30" align="center">THU<br>      </td>
        <td valign="top" width="100" align="center">1:00PM<br>      </td>
        <td valign="top" width="100" align="center">3:30PM<br>      </td>
        <td valign="top" width="100" align="center">E2<br></td>
                <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=1126&type=1')">FP102-FIM CS.pdf</a><br /></td>
                  <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=470&type=2')">FP102-Assignment 1 Brief.pdf</a><br />II <a href="#" onclick="return popup('/icom/faculty/viewer/?id=471&type=2')">FP102-Assignment 2 Brief.pdf</a><br />III <a href="#" onclick="return popup('/icom/faculty/viewer/?id=788&type=2')">FP102-Peer Assessment Sheet.pdf</a><br /></td>  
      </tr>
          <tr>
                <td valign="top" width="100"><a href="javascript:void(0)" onclick="return popup2('/icom/student/main/report?varCrs_ID=GE030-001&varSemester_code=1/20&prog_id=FIM')">GE030-001</a></td>
        <td valign="top" width="150">ELECTRONICS & COMPUTER SYSTEMS </td>
        <td valign="top" width="100" align="center">ZHZ<br>(<a href="mailto:fafafer@gmail.com">abcdef@gmail.com</a>)        </td>
        <td valign="top" width="100" align="center">3 </td>
                <td valign="top" width="30" align="center">MON<br>WED<br>       </td>
        <td valign="top" width="100" align="center">7:30PM<br>      </td>
        <td valign="top" width="100" align="center">8:45PM<br>      </td>
        <td valign="top" width="100" align="center">C2<br></td>
                <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=1203&type=1')">GE030-FIM CS.pdf</a><br /></td>
                  <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=474&type=2')">GE030-Assignment Brief.pdf</a><br />II <a href="#" onclick="return popup('/icom/faculty/viewer/?id=684&type=2')">GE030-Oral Presentation Brief.pdf</a><br /></td>  
      </tr>
          <tr>
                <td valign="top" width="100"><a href="javascript:void(0)" onclick="return popup2('/icom/student/main/report?varCrs_ID=HM010-001&varSemester_code=1/20&prog_id=FIM')">HM010-001</a></td>
        <td valign="top" width="150">SURVEY OF POP MUSIC </td>
        <td valign="top" width="100" align="center">IBS<br>(<a href="mailto:abcdef@icom.edu.my">abcder@icom.edu.my</a>)     </td>
        <td valign="top" width="100" align="center">2 </td>
                <td valign="top" width="30" align="center">FRI<br>      </td>
        <td valign="top" width="100" align="center">11:00AM<br>     </td>
        <td valign="top" width="100" align="center">12:40PM<br>     </td>
        <td valign="top" width="100" align="center">RH<br></td>
                <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=1220&type=1')">HM010-FIM CS.pdf</a><br /></td>
                  <td valign="top" align="left"></td>  
      </tr>
          <tr>
                <td valign="top" width="100"><a href="javascript:void(0)" onclick="return popup2('/icom/student/main/report?varCrs_ID=MT010-001&varSemester_code=1/20&prog_id=FIM')">MT010-001</a></td>
        <td valign="top" width="150">MUSIC TECHNOLOGY & MIDI SYSTEMS </td>
        <td valign="top" width="100" align="center">SKS<br>(<a href="mailto:faer@icom.edu.my">sdferfs@icom.edu.my</a>)      </td>
        <td valign="top" width="100" align="center">2 </td>
                <td valign="top" width="30" align="center">TUE<br>THU<br>       </td>
        <td valign="top" width="100" align="center">12:00PM<br>     </td>
        <td valign="top" width="100" align="center">12:50PM<br>     </td>
        <td valign="top" width="100" align="center">C3<br></td>
                <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=1131&type=1')">MT010-FIM CS.pdf</a><br /></td>
                  <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=776&type=2')">MT010-Project Brief.pdf</a><br />II <a href="#" onclick="return popup('/icom/faculty/viewer/?id=775&type=2')">MT010-Assignment Brief.pdf</a><br /></td>  
      </tr>
          <tr>
                <td valign="top" width="100"><a href="javascript:void(0)" onclick="return popup2('/icom/student/main/report?varCrs_ID=PF011-001&varSemester_code=1/20&prog_id=FIM')">PF011-001</a></td>
        <td valign="top" width="150">PERFORMANCE SEMINAR </td>
        <td valign="top" width="100" align="center">IBS<br>(<a href="mailto:fafafa@icom.edu.my">fafafaf@icom.edu.my</a>)        </td>
        <td valign="top" width="100" align="center">0 </td>
                <td valign="top" width="30" align="center"><br>     </td>
        <td valign="top" width="100" align="center">BY APPT<br>     </td>
        <td valign="top" width="100" align="center">BY APPT<br>     </td>
        <td valign="top" width="100" align="center">BY APPT<br></td>
                <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=1208&type=1')">PF011-PF031-FIM CS.pdf</a><br /></td>
                  <td valign="top" align="left"></td>  
      </tr>
          <tr>
                <td valign="top" width="100"><a href="javascript:void(0)" onclick="return popup2('/icom/student/main/report?varCrs_ID=PI041-001&varSemester_code=1/20&prog_id=FIM')">PI041-001</a></td>
        <td valign="top" width="150">VOICE </td>
        <td valign="top" width="100" align="center">IBS<br>(<a href="mailto:fafafa@icom.edu.my">fafan@icom.edu.my</a>)      </td>
        <td valign="top" width="100" align="center">1 </td>
                <td valign="top" width="30" align="center"><br>     </td>
        <td valign="top" width="100" align="center">BY APPT<br>     </td>
        <td valign="top" width="100" align="center">BY APPT<br>     </td>
        <td valign="top" width="100" align="center">BY APPT<br></td>
                <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=1143&type=1')">PI041-FIM CS.pdf</a><br /></td>
                  <td valign="top" align="left">I <a href="#" onclick="return popup('/icom/faculty/viewer/?id=795&type=2')">PI0XX mark sheet.pdf</a><br /></td>  
      </tr>
          <tr>
                <td valign="top" width="100"><a href="javascript:void(0)" onclick="return popup2('/icom/student/main/report?varCrs_ID=PL001-001&varSemester_code=1/20&prog_id=FIM')">PL001-001</a></td>
        <td valign="top" width="150">English Placement </td>
        <td valign="top" width="100" align="center">VNS<br>(<a href="mailto:xxx@gmail.com">abcder@gmail.com</a>)        </td>
        <td valign="top" width="100" align="center">0 </td>
                <td valign="top" width="30" align="center"><br>     </td>
        <td valign="top" width="100" align="center">-<br>       </td>
        <td valign="top" width="100" align="center">-<br>       </td>
        <td valign="top" width="100" align="center">-<br></td>
                <td valign="top" align="left"></td>

【问题讨论】:

    标签: python selenium pdf


    【解决方案1】:

    您可以尝试对 python 使用正则表达式模式匹配

    import re
    path = re.compile("/icom/faculty/viewer/.[a-z]{2}.*")
    lst = path.findall(sauce)
    print(lst)
    

    lst 将是一个列表,其中包含所有“/icom/faculty/viewer/?id”字符串,其中包含提到的匹配项,后跟任何字符,直到遇到新行。 所以...你应该得到一个匹配字符串和唯一 index=xxx 值的字符串列表。

    您可以遍历此列表,并为您想要的每个分配进一步将其分成更小的列表。

    【讨论】:

      【解决方案2】:

      如果我的问题是正确的,你需要一些字符串匹配的逻辑。

      这是一种可能的方法。

      您检查一个字符串是否在另一个字符串中并继续执行您的逻辑,否则您可以定义一些例外。

      link = "/icom/faculty/viewer/?id=1260&type=2"
      word = "/icom/faculty/viewer/?id"
      
      if word in link:
         print('word is in link')
      else:
         print('word is not in link')
      

      如果你提到字符串连接,我推荐这个线程:

      Which is the preferred way to concatenate a string in Python?

      另外,如果你想抓取多个字符串的字符串,你可以定义一个简单的for循环。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多