티스토리 수익 글 보기

티스토리 수익 글 보기

Fix index out of range when <body> or <head> is missing by FVolral · Pull Request #272 · lxml/lxml · GitHub
Skip to content

Fix index out of range when <body> or <head> is missing – #272

Closed
FVolral wants to merge 1 commit into
lxml:masterfrom
FVolral:fix-html-index-out-of-range
Closed

Fix index out of range when <body> or <head> is missing#272
FVolral wants to merge 1 commit into
lxml:masterfrom
FVolral:fix-html-index-out-of-range

Conversation

@FVolral

@FVolral FVolral commented Sep 2, 2018

Copy link
Copy Markdown

If a HTML file that doesn’t contain a or part is parsed,
it can lead to a index out of range error when you try
getattr(lxmlElement, "body")

If a HTML file that doesn't contain <body> is parsed, it can lead
to a index out of range error when you try getattr(lxmlElement, "body")
for example.

@scoder scoder left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally a good idea, but the implementation needs fixing. Would have been obvious with some tests (hint, hint).

Comment thread src/lxml/html/__init__.py
return self.xpath('//head|//x:head', namespaces={'x':XHTML_NAMESPACE})[0]
result = self.xpath('//head|//x:head', namespaces={'x':XHTML_NAMESPACE})
if len(result) > 1:
return result[0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise … do what? Return None? Would be good to say so if that’s what you want.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve some difficulty to run lxml tests, it’s a bit new for me, and that pull request is a kind of experience. It should return None indeed, I can correct that.

Comment thread src/lxml/html/__init__.py
"""
return self.xpath('//head|//x:head', namespaces={'x':XHTML_NAMESPACE})[0]
result = self.xpath('//head|//x:head', namespaces={'x':XHTML_NAMESPACE})
if len(result) > 1:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why > 1 ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“> 0”, because I’m stupid :p

@scoder

scoder commented Sep 2, 2018

Copy link
Copy Markdown
Member

No problem. To start the tests, it should be enough to run python test.py. The relevant ones are in src/lxml/html/tests/test_basic.txt. Doctests.

@lxml lxml deleted a comment from charshack Jul 15, 2021
@scoder scoder closed this in c0379b3 Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants