首页 > 解决方案 > 需要一些简单的python在字符串中查找的帮助

问题描述

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

概括

我正在用硒做一个网络自动化项目。这个python项目的主要目标是

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

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

步骤 3。对 page_source 字符串上的 PDF查看器链接进行匹配搜索

第 4 步。导航到匹配的 PDF查看器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))

但是 find 的输出是5122

我希望str.find使用匹配并列出所有匹配结果并输出行中的值而不是数字...

所以......我该怎么做?我在互联网上搜索了这个,但对我来说仍然没有太多信息。对不起,我只是编程的初学者,我的英语不太好。先感谢您。

顺便说一句,我的脑海里突然出现了一个问题。

如果我能够匹配Step 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>

标签: pythonseleniumpdf

解决方案


如果我的问题是正确的,那么您需要一些用于字符串匹配的逻辑。

这是一种可能的方法。

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

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')

如果您参考字符串连接,我推荐这个线程:

在 Python 中连接字符串的首选方法是哪种?

此外,如果您想获取多个字符串的字符串,您可以定义一个简单的 for 循环。


推荐阅读