遵循维基百科链接的 python 程序中的美丽汤错误

问题描述 投票:0回答:0

我创建了一个 python 程序来访问维基百科文章中的第一个链接。我设法消除了所有不正确的链接,以便它会遵循实际的第一个“真实”链接。然后该程序将跟随第一个链接到另一个维基百科页面。程序成功找到第一个链接,但程序成功找到第一个链接,但在尝试访问该链接时发现错误。

我认为问题可能出在本节代码的最后

for link in AllLinks:
    # We are only interested in other wiki articles
    if (link not in nonolist):
      try:
        if link["href"].find("/wiki/") != -1 and link["href"].find(
            "/wiki/Help") == -1 and link["href"].find(
              "/wiki/Wikipedia:") == -1 and link["href"].find(
                "wiktionary.org") == -1:
          print(link)
          Organiser(str(link))
          break
      except:
        print("ERROR!!!!!")
        continue

主办方功能开始

def Organiser(raw):
  global soup
  global page
  if "href" not in raw:
    page = raw
    print("the raw page it finds is " + page)
  else:
    for a in soup.find_all('a', href=True):
      if "wiki" in str(a):
        page = a['href']
        print(a['href'])
        print("found an a href link containing" + page)
        break
      else:
        print("moving to next link")

  response = requests.get(url=page)
  soup = BeautifulSoup(response.content, 'html.parser')
  title = soup.title.get_text()

这是代码的链接 https://replit.com/@KarlMarxSoul/SpiderHeck-1#main.py

python beautifulsoup wikipedia
© www.soinside.com 2019 - 2024. All rights reserved.