【问题标题】:Unscriptable Int Error for String Slice字符串切片的不可编写的 Int 错误
【发布时间】:2011-03-27 21:06:56
【问题描述】:

我正在编写一个网络爬虫,我有一个表格,里面有我想要下载、保存和稍后分析的 .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[以及其他意外进入我的字符串的注释内容]

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

  • ://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' object is unsubscriptable)

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

【问题讨论】:

    标签: python string slice


    【解决方案1】:
    just_the_link = end_of_link[9:end_of_link]
    

    这是您的问题,就像错误消息中所说的那样。 end_of_link 是一个整数——pdf_link_string 中“.pdf”的索引,您在上一行中计算了它。所以自然不能对其进行切片。你想切片pdf_link_string

    【讨论】:

      【解决方案2】:

      听起来像是正则表达式的工作:

      import urllib, urllib2, re
      
      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:
            pdfURLPattern = re.compile("""://(\w+/)+\S+.pdf""")
            pdfURLMatch = pdfURLPattern.search(line)
      
      #If there is no match than search() returns None, otherwise the whole group (group(0)) returns the URL of interest.
            if pdfURLMatch:
               print pdfURLMatch.group(0)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-27
        • 2015-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-09
        • 1970-01-01
        相关资源
        最近更新 更多