Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use chardet by default to find the encoding #2040

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lncrawl/core/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def submit_form_json(
)
return response.json()

def get_soup(self, url, headers={}, parser=None, **kwargs) -> BeautifulSoup:
def get_soup(self, url, headers={}, parser=None, encoding=None, **kwargs) -> BeautifulSoup:
"""Fetch the content and return a BeautifulSoup instance of the page"""
headers = CaseInsensitiveDict(headers)
headers.setdefault(
Expand All @@ -293,10 +293,10 @@ def get_soup(self, url, headers={}, parser=None, **kwargs) -> BeautifulSoup:
)
response = self.get_response(url, **kwargs)
self.last_soup_url = url
return self.make_soup(response, parser)
return self.make_soup(response, parser, encoding)

def post_soup(
self, url, data={}, headers={}, parser=None, **kwargs
self, url, data={}, headers={}, parser=None, encoding=None, **kwargs
) -> BeautifulSoup:
"""Make a POST request and return BeautifulSoup instance of the response"""
headers = CaseInsensitiveDict(headers)
Expand All @@ -305,10 +305,10 @@ def post_soup(
"text/html,application/xhtml+xml,application/xml;q=0.9",
)
response = self.post_response(url, data=data, headers=headers, **kwargs)
return self.make_soup(response, parser)
return self.make_soup(response, parser, encoding)

def submit_form_for_soup(
self, url, data={}, headers={}, multipart=False, parser=None, **kwargs
self, url, data={}, headers={}, multipart=False, parser=None, encoding=None, **kwargs
) -> BeautifulSoup:
"""Simulate submit form request and return a BeautifulSoup instance of the response"""
headers = CaseInsensitiveDict(headers)
Expand All @@ -319,4 +319,4 @@ def submit_form_for_soup(
response = self.submit_form(
url, data=data, headers=headers, multipart=multipart, **kwargs
)
return self.make_soup(response, parser)
return self.make_soup(response, parser, encoding)
11 changes: 9 additions & 2 deletions lncrawl/core/soup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from .exeptions import LNException

import chardet

logger = logging.getLogger(__name__)


Expand All @@ -33,11 +35,16 @@ def make_soup(
self,
data: Union[Response, bytes, str],
parser: Optional[str] = None,
encoding: Optional[str] = None,
) -> BeautifulSoup:
if isinstance(data, Response):
html = data.content.decode("utf8", "ignore")
if encoding is None:
encoding = chardet.detect(data.content)["encoding"]
html = data.content.decode(encoding, "ignore")
elif isinstance(data, bytes):
html = data.decode("utf8", "ignore")
if encoding is None:
encoding = chardet.detect(data)["encoding"]
html = data.decode(encoding, "ignore")
elif isinstance(data, str):
html = str(data)
else:
Expand Down
Loading