beginner web scraping code iteration issue

Viewed 35

I am new to Python and would really appreciate some help!!

I have been trying to create a dictionary for assigning books to their authors, only for it to come out messy and be repeating itself.

How can I fix this?

import requests
from bs4 import BeautifulSoup

url = "https://www.banyen.com/new-arrivals/index.html"
response = requests.get(url)
html = response.content
scraped = BeautifulSoup(html,'html.parser')
results = []

article = scraped.find("div", class_="block block-system block-odd clearfix")
for i in article.find_all():
    name = i.find("h2", "a href", class_="teaser-title")
    author = i.find("span", class_="price-amount")
    if name is not None:
        if author is not None:
          results.append({name:author})

print(results)
1 Answers
import requests
from bs4 import BeautifulSoup
import re

url = "https://www.banyen.com/new-arrivals/index.html"
response = requests.get(url)
html = response.content
scraped = BeautifulSoup(html,'html.parser')
results = []

articles = scraped.find_all("div", id=re.compile("node-"))
for i in articles:
    name = i.find("h2").find('a')
    author = i.find("span", class_="price-amount")
    if name is not None:
        if author is not None:
            results.append({name.text.strip():author.text})

print(results)
Related