Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex: caret character
#1
• A single function, getLinks, that takes in a Wikipedia article URL of the form /
wiki/<Article_Name> and returns a list of all linked article URLs in the same
form.
• A main function that calls getLinks with some starting article, chooses a random
article link from the returned list, and calls getLinks again, until we stop the
program or until there are no article links found on the new page.

code:
import requests
from bs4 import BeautifulSoup
import re
import datetime
import random

random.seed(datetime.datetime.now())
def getLinks(articleUrl):
    html = requests.get("http://en.wikipedia.org" + articleUrl)
    bsObj = BeautifulSoup(html.content, 'html.parser')
    return bsObj.find("div", {"id":"bodyContent"}).find_all("a", href=re.compile("^(/wiki/)((?!:).)*$"))
links = getLinks("/wiki/Kevin_Bacon")
while len(links) > 0:
	newArticle = links[random.randint(0, len(links)-1)].attrs["href"]
	print(newArticle)
	links = getLinks(newArticle)
line 11 has caret character. What I don't understand is why is it used. The point of this program is to find all links on Kevin Bacon WP page that lead to other articles. These urls have "wiki" so why is then a negative character class used? Interestingly, when I removed caret character results made more sense. Not sure why author of the book used, I hope that you can figure this out. Idea
Reply
#2
It's not a negative character class. ^ only indicates a negative character class if it is the first character in brackets. Otherwise it matches the beginning of the string. So that only matches strings that start with '/wiki/'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Interesting, read several sources on regex and didn't find that explanation. Thanks.
Reply
#4
It's in the Python re documentation, which is where I learned regexes from.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020