使用 Beautiful Soup 查找特定字母前的链接

Using Beautiful Soup to Find Links Before a Certain Letter

我有一个 BeautifulSoup 问题,希望您能帮我解决。

目前,我有一个网站,上面有很多 link。 link 指向包含 linked 项目数据的页面。如果你想看看,就是这个:http://ogle.astrouw.edu.pl/ogle4/ews/ews.html。我最终想要完成的是打印出标有 'N' 的数据的 links。一开始可能不明显,但如果你仔细看网站,有些数据的星号后面有 'N',有些则没有。之后,我使用 link 下载包含我需要的有关该数据的信息的文件。该网站非常方便,因为下载 URLs 只是从数据到数据更改了一点,所以我只需要更改 URL 的一部分,您将在下面的代码中看到。

我目前已经完成了数据下载部分。然而,这就是你进来的地方。目前,我需要输入我想要的 BLG 活动的标识号。 (查看下面的代码后,这一点就会变得很明显。)但是,该网站会随着时间的推移不断更新,并且必须手动搜索 'N' 事件会占用不必要的时间。我希望 Python 代码能够为我做这件事。我对这个主题的最初想法是,我可以 BeautifulSoup 搜索所有 N 的文本,但我 运行 解决了实现该问题的一些问题。我觉得我对 BeautifulSoup 不够熟悉,无法完成我想完成的事情。一些帮助将不胜感激。

我目前的代码如下。作为示例,我已经放入了带有 'N' 标签的 运行ge BLG 事件。

#Retrieve .gz files from URLs

from urllib.request import urlopen
import urllib.request
from bs4 import BeautifulSoup

#Access website
URL = 'http://ogle.astrouw.edu.pl/ogle4/ews/ews.html'
soup = BeautifulSoup(urlopen(URL))

#Select the desired data numbers 
numbers = list(range(974,998)) 
x=0
for i in numbers:
    numbers[x] = str(i)
    x += 1
print(numbers)

#Get all links and put into list
allLinks = []
for link in soup.find_all('a'):
    list_links = link.get('href')
    allLinks.append(list_links)

#Remove None datatypes from link list
while None in allLinks:
    allLinks.remove(None)
#print(allLinks)

#Remove all links but links to data pages and gets rid of the '.html'
list_Bindices = [i for i, s in enumerate(allLinks) if 'b' in s]
print(list_Bindices)
bLinks = []
for x in list_Bindices:
    bLinks.append(allLinks[x])
bLinks = [s.replace('.html', '') for s in bLinks]
#print(bLinks)

#Create a list of indices for accessing those pages
list_Nindices = []
for x in numbers:
    list_Nindices.append([i for i, s in enumerate(bLinks) if x in s])
#print(type(list_Nindices))
#print(list_Nindices)

nindices_corrected = []
place = 0
while place < (len(list_Nindices)):
    a = list_Nindices[place]
    nindices_corrected.append(a[0])
    place = place + 1
#print(nindices_corrected)

#Get the page names (without the .html) from the indices
nLinks = []
for x in nindices_corrected:
    nLinks.append(bLinks[x])
#print(nLinks)

#Form the URLs for those pages
final_URLs = []
for x in nLinks:
    y = "ftp://ftp.astrouw.edu.pl/ogle/ogle4/ews/2017/"+ x + "/phot.dat"
    final_URLs.append(y)
#print(final_URLs)
#Retrieve the data from the URLs
z = 0
for x in final_URLs:
    name = nLinks[z] + ".dat"
    #print(name)
    urllib.request.urlretrieve(x, name)
    z += 1
#hrm = urllib.request.urlretrieve("ftp://ftp.astrouw.edu.pl/ogle/ogle4/ews/2017/blg-0974.tar.gz", "practice.gz")

这段代码花了我很多时间来编写,因为我不是专业的程序员,也不是 BeautifulSoup 或 URL 任何操作方面的专家。其实我用MATLAB比Python还多。因此,我倾向于从 MATLAB 的角度来思考,t运行 会变成效率较低的 Python 代码。但是,效率是 而不是 我在这个问题中寻找的东西。我可以多等五分钟让我的代码完成,如果这意味着我了解正在发生的事情并且可以完成我需要完成的事情。感谢您提供的任何帮助!我意识到这是一个相当多方面的问题。

应该这样做:

from urllib.request import urlopen
import urllib.request
from bs4 import BeautifulSoup

#Access website
URL = 'http://ogle.astrouw.edu.pl/ogle4/ews/ews.html'
soup = BeautifulSoup(urlopen(URL), 'html5lib')

在这里,我使用 html5lib 来解析 url 内容。

接下来,我们将查看 table,如果星星名称中有 'N',则提取链接:

table = soup.find('table')

links = []
for tr in table.find_all('tr', {'class' : 'trow'}):
    td = tr.findChildren()
    if 'N' in td[4].text:
        links.append('http://ogle.astrouw.edu.pl/ogle4/ews/' + td[1].a['href'])


print(links)

输出:

['http://ogle.astrouw.edu.pl/ogle4/ews/blg-0974.html', 'http://ogle.astrouw.edu.pl/ogle4/ews/blg-0975.html', 'http://ogle.astrouw.edu.pl/ogle4/ews/blg-0976.html', 'http://ogle.astrouw.edu.pl/ogle4/ews/blg-0977.html', 'http://ogle.astrouw.edu.pl/ogle4/ews/blg-0978.html', 
...
]