【发布时间】:2020-04-24 01:12:08
【问题描述】:
所以在我的 Python 函数中,我传递了一个 url,在该 url 上搜索 pdf 文件,然后下载这些文件。在大多数情况下,它都能完美运行。
def get_pdfs(my_url):
html = urllib2.urlopen(my_url).read()
html_page = BeautifulSoup(html)
current_link = ''
links = []
for link in html_page.find_all('a'):
current_link = link.get('href')
if current_link.endswith('pdf'):
print(current_link)
links.append(my_url + current_link)
#print(links)
for link in links:
#urlretrieve(link)
wget.download(link)
get_pdfs('https://grader.eecs.jacobs-university.de/courses/320241/2019_2/')
但是,当我尝试将我的功能用于特定课程网站时,我的 current_link 是
/courses/320241/2019_2/lectures/lecture_7_8.pdf
虽然它应该被自动检测到并且应该只是
lectures/lecture_7_8.pdf
而我传递给函数的原始 my_url 是
https://grader.eecs.jacobs-university.de/courses/320241/2019_2/
由于我同时添加了它们并且部分链接重复,因此下载的文件已损坏。如何检查current_link 是否与my_url 重复任何部分,如果是,如何在下载前将其删除?
【问题讨论】:
标签: python string parsing web-scraping beautifulsoup