Skip to content

Commit

Permalink
FIX: find mode=first returns None
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhumber committed Apr 21, 2020
1 parent 7a9577c commit 76f65ba
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pip install -U gazpacho

---

#### 0.9.2 (2020-04-21)

- Fixed `find(..., mode='first')` to return `None` and not an `IndexError` (thanks, [psyonara](https://github.com/maxhumber/gazpacho/issues/14)!)

#### 0.9.1 (2020-02-16)

- Fixed `UnicodeEncodeError` lurking beneath `get` (thanks for the "Issue" [mlehotay](https://github.com/mlehotay)!)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ print(df[['PLAYER', 'TEAM', 'SALARY', 'AGE']].head(3))

# PLAYER TEAM SALARY AGE
# 0 1. Mitchell Marner TOR $16,000,000 22
# 1 2. Auston Matthews TOR $15,900,000 21
# 2 3. John Tavares TOR $15,900,000 28
# 1 2. John Tavares TOR $15,900,000 28
# 2 3. Auston Matthews TOR $15,900,000 21
```

Powered by gazpacho:
Expand All @@ -151,8 +151,8 @@ print(df[['PLAYER', 'TEAM', 'SALARY', 'AGE']].head(3))

# PLAYER TEAM SALARY AGE
# 0 1. Mitchell Marner TOR $16,000,000 22
# 1 2. Auston Matthews TOR $15,900,000 21
# 2 3. John Tavares TOR $15,900,000 28
# 1 2. John Tavares TOR $15,900,000 28
# 2 3. Auston Matthews TOR $15,900,000 21
```


Expand Down
2 changes: 2 additions & 0 deletions gazpacho/soup.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ def find(self, tag, attrs=None, mode="auto", strict=False):
self.group = 0
self.groups = []
self.feed(self.html)
if mode in ["auto", "first"] and not self.groups:
return None
if mode == "all":
return self.groups
if mode == "first":
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="gazpacho",
version="0.9.1",
version="0.9.2",
description="gazpacho is a web scraping library",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ def test_get_params():


def test_weird_characters():
url = 'https://httpbin.org/anything/drãke'
url = "https://httpbin.org/anything/drãke"
content = get(url)
assert url == json.loads(content)['url']
assert url == json.loads(content)["url"]
20 changes: 19 additions & 1 deletion tests/test_soup.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_find_nested_empty_tag(fake_html_3):
def test_find_mutliple_imgs(fake_html_3):
soup = Soup(fake_html_3)
result = soup.find("img")
assert result[1].attrs['src'] == 'bye.jpg'
assert result[1].attrs["src"] == "bye.jpg"


def test_remove_tags(fake_html_4):
Expand All @@ -131,3 +131,21 @@ def test_remove_tags_no_strip(fake_html_4):
result
== "\n \n I like soup and I really like cold soup\n I guess hot soup is okay too\n \n "
)


def test_find_no_match_first(fake_html_1):
soup = Soup(fake_html_1)
result = soup.find("a", mode="first")
assert result == None


def test_find_no_match_all(fake_html_1):
soup = Soup(fake_html_1)
result = soup.find("a", mode="all")
assert result == []


def test_find_no_match_auto(fake_html_1):
soup = Soup(fake_html_1)
result = soup.find("a", mode="auto")
assert result == None

0 comments on commit 76f65ba

Please sign in to comment.