python - 字符串切片的不可编写脚本的 Int 错误

标签 python string slice

我正在编写一个网络爬虫,并且有一个表格,其中充满了我想要下载、保存并稍后分析的 .pdf 文件的链接。我用的是漂亮的汤,我让汤找到了所有的链接。它们通常是漂亮的汤标签对象,但我把它们变成了字符串。该字符串实际上是一堆垃圾,链接文本埋在中间。我想删掉那些垃圾,只留下链接。然后我会把它们变成一个列表,稍后让 python 下载它们。 (我的计划是让 python 保留 pdf 链接名称的列表,以跟踪下载的内容,然后它可以根据这些链接名称或其一部分来命名文件)。

但是 .pdf 文件的名称长度是可变的,例如:

  • I_am_the_first_file.pdf
  • And_I_am_the_seond_file.pdf

由于它们存在于表中,因此它们有一堆垃圾文本:

  • a href = ://blah/blah/blah/I_am_the_first_file.pdf[加上其他意外进入我的字符串的注释内容]
  • a href = ://blah/blah/blah/And_I_am_the_seond_file.pdf[加上其他意外进入我的字符串的注释内容]

所以我想切掉(“切片”)字符串的前半部分和最后一部分,只留下指向我的网址的字符串(所以接下来是我的程序所需的输出):

  • ://blah/blah/blah/I_am_the_first_file.pdf
  • ://blah/blah/blah/And_I_am_the_seond_file.pdf

正如您所看到的,第二个文件的字符串中的字符比第一个文件多。所以我不能这样做:

string[9:40]

或者其他任何东西,因为这适用于第一个文件,但不适用于第二个文件。

所以我试图为字符串切片的末尾找到一个变量,如下所示:

string[9:x]

其中 x 是以“.pdf”结尾的字符串中的位置(我的想法是使用 string.index('.pdf') 函数来执行此操作。

但是 t3h 会失败吗,因为我在尝试使用变量来执行此操作时遇到错误

("TypeError: 'int' object is unsubscriptable")

除了弄乱字符串之外,可能有一个简单的答案和更好的方法来做到这一点,但是你们比我聪明得多,我想你们会立即知道。

这是迄今为止我的完整代码:

import urllib, urllib2

from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen("mywebsite.com")

soup = BeautifulSoup(page)

table_with_my_pdf_links = soup.find('table', id = 'searchResults')
#"search results" is just what the table i was looking for happened to be called.

for pdf_link in table_with_my_pdf_links.findAll('a'):
#this says find all the links and looop over them

   pdf_link_string = str(pdf_link)
#turn the links into strings (they are usually soup tag objects, which don't help me much that I know of)

   if 'pdf' in pdf_link_string:
#some links in the table are .html and I don't want those, I just want the pdfs.

      end_of_link = pdf_link_string.index('.pdf')
#I want to know where the .pdf file extension ends because that's the end of the link, so I'll slice backward from there

      just_the_link = end_of_link[9:end_of_link]
#here, the first 9 characters are junk "a href = yadda yadda yadda".  So I'm setting a variable that starts just after that junk and goes to the .pdf (I realize that I will actualy have to do .pdf + 3 or something to actually get to the end of string, but this makes it easier for now).

      print just_the_link
#I debug by print statement because I'm an amatuer

行(倒数第二行)如下: just_the_link = end_of_link[9:end_of_link]

返回错误(TypeError:'int'对象不可订阅)

另外,“:”应该是超文本传输​​协议(protocol)冒号,但它不允许我发布 b/c 新手不能发布超过 2 个链接,所以我把它们删除了。

最佳答案

just_the_link = end_of_link[9:end_of_link]

这是你的问题,就像错误消息所说的那样。 end_of_link 是一个整数 - pdf_link_string 中“.pdf”的索引,您在前一行中计算出该索引。所以您自然可以'不要把它切片。您想要对 pdf_link_string 进行切片。

关于python - 字符串切片的不可编写脚本的 Int 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5452464/

相关文章:

python - 最后一个子进程调用在 FFMPEG 的串联代码中不起作用。我应该如何解决这个问题?

python - QtWebKit内存泄漏有什么解决方案吗?

php - 简单的preg_replace

loops - 指针 slice 和循环

python - 数半金字塔 Python

python - psycopg2 "select for update"

java - 将内容附加到 StringBuffer 对象时无法实现换行 usint "\n"

android - 从字符串中删除空格

go - 在 Go 中分配 slice 与重新声明 slice

javascript - 关于 JavaScript 的 slice 和 splice 方法的问题