字符串切片的不可脚本化整数错误
我正在写一个网页抓取工具,想要下载、保存并分析一堆链接到 .pdf 文件的表格。我使用了 Beautiful Soup 来找到所有的链接。通常这些链接是 Beautiful Soup 的标签对象,但我把它们转成了字符串。这个字符串里面其实有很多杂乱的内容,链接的文本被埋在中间。我想把这些杂乱的内容去掉,只留下链接。然后我会把这些链接放到一个列表里,之后用 Python 来下载它们。(我的计划是让 Python 保持一个 pdf 链接名称的列表,以便跟踪它下载了哪些文件,然后可以根据这些链接名称或其中的一部分来命名文件)。
不过,这些 .pdf 文件的名字长度不一样,比如:
- I_am_the_first_file.pdf
- And_I_am_the_second_file.pdf
在表格中,它们的链接前面有一堆杂乱的文本:
- a href = ://blah/blah/blah/I_am_the_first_file.pdf[还有其他一些注释内容,意外地混进了我的字符串里]
- a href = ://blah/blah/blah/And_I_am_the_second_file.pdf[还有其他一些注释内容,意外地混进了我的字符串里]
所以我想把字符串的前面和后面那部分切掉,只留下指向我的网址的部分(以下是我程序想要的输出):
://blah/blah/blah/I_am_the_first_file.pdf
://blah/blah/blah/And_I_am_the_second_file.pdf
不过你可以看到,第二个文件的字符串比第一个多了几个字符。所以我不能这样做:
string[9:40]
因为这样只适用于第一个文件,而不适用于第二个。
所以我在想要为字符串切割的结束位置定义一个变量,像这样:
string[9:x]
其中 x 是字符串中以 '.pdf' 结尾的位置(我想用 string.index('.pdf') 函数来实现这个)。
但这可能会失败,因为我在尝试用变量来做这件事时遇到了错误
("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
)
另外,":" 应该是超文本传输协议的冒号,但我不能发这个,因为新手不能发超过两个链接,所以我把它们删掉了。
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)
just_the_link = end_of_link[9:end_of_link]
这是你的问题,就像错误信息所说的那样。 end_of_link
是一个整数——它表示在 pdf_link_string
中 ".pdf" 的位置索引,这个索引是在前面的那一行计算出来的。所以你自然不能用它来切片。你应该切片的是 pdf_link_string
。