Skip to content

Commit a633f0d

Browse files
CaedenPHgithub-actions
authored andcommitted
Fix get amazon product data erroring due to whitespace in headers (TheAlgorithms#9009)
* updating DIRECTORY.md * fix(get-amazon-product-data): Remove whitespace in headers * refactor(get-amazon-product-data): Don't print to_csv --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 2b1b8ec commit a633f0d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

web_programming/get_amazon_product_data.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ def get_amazon_product_data(product: str = "laptop") -> DataFrame:
1919
"""
2020
url = f"https://www.amazon.in/laptop/s?k={product}"
2121
header = {
22-
"User-Agent": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36
23-
(KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""",
22+
"User-Agent": (
23+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
24+
"(KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36"
25+
),
2426
"Accept-Language": "en-US, en;q=0.5",
2527
}
26-
soup = BeautifulSoup(requests.get(url, headers=header).text)
28+
soup = BeautifulSoup(requests.get(url, headers=header).text, features="lxml")
2729
# Initialize a Pandas dataframe with the column titles
2830
data_frame = DataFrame(
2931
columns=[
@@ -74,8 +76,8 @@ def get_amazon_product_data(product: str = "laptop") -> DataFrame:
7476
except ValueError:
7577
discount = float("nan")
7678
except AttributeError:
77-
pass
78-
data_frame.loc[len(data_frame.index)] = [
79+
continue
80+
data_frame.loc[str(len(data_frame.index))] = [
7981
product_title,
8082
product_link,
8183
product_price,

0 commit comments

Comments
 (0)