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

Fix get amazon product data erroring due to whitespace in headers #8971

Closed
Closed
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
1 change: 1 addition & 0 deletions Amazon Product Data for headphones.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
,Product Title,Product Link,Current Price of the product,Product Rating,MRP of the product,Discount
18 changes: 12 additions & 6 deletions web_programming/get_amazon_product_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def get_amazon_product_data(product: str = "laptop") -> DataFrame:
"""
url = f"https://www.amazon.in/laptop/s?k={product}"
header = {
"User-Agent": """Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36
(KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36""",
"User-Agent": (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
"(KHTML, like Gecko)Chrome/44.0.2403.157 Safari/537.36"
),
"Accept-Language": "en-US, en;q=0.5",
}
soup = BeautifulSoup(requests.get(url, headers=header).text)
soup = BeautifulSoup(requests.get(url, headers=header).text, features="lxml")
# Initialize a Pandas dataframe with the column titles
data_frame = DataFrame(
columns=[
Expand Down Expand Up @@ -74,8 +76,8 @@ def get_amazon_product_data(product: str = "laptop") -> DataFrame:
except ValueError:
discount = float("nan")
except AttributeError:
pass
data_frame.loc[len(data_frame.index)] = [
continue
data_frame.loc[str(len(data_frame.index))] = [
product_title,
product_link,
product_price,
Expand All @@ -97,4 +99,8 @@ def get_amazon_product_data(product: str = "laptop") -> DataFrame:

if __name__ == "__main__":
product = "headphones"
get_amazon_product_data(product).to_csv(f"Amazon Product Data for {product}.csv")
print(
get_amazon_product_data(product).to_csv(
f"Amazon Product Data for {product}.csv"
)
)