我的代码基于Python中的Web Scraping概念。我的代码正在运行。即使不显示任何输出语句,它也不会显示任何错误。我还有另外2个文件,它只包含有关locator的信息。我认为这些文件是不需要的。所以我没有贴在这里。你能帮我吗?
part-1
import requests
from books_pages.whole_page import Allbooks
page_content=requests.get('Some website name').content
page=Allbooks(page_content)
everybooks=page.books
for book in everybooks:
print(book)
part-2
from bs4 import BeautifulSoup
from locator_books.whole_pages import List_allbooks_locator
from parser_book.Parsing_book_detail import Individual_book_detail
class Allbooks():
def __init__(self,page_content):
self.soup=BeautifulSoup(page_content,'html.parser')
@property
def books(self):
location=List_allbooks_locator.ALLBOOKS
books=self.soup.select(location)
return [Individual_book_detail(book) for book in books]
part-3
import re
from locator_books.indivdual_book import Book_locator
class Individual_book_detail():
RATING={
'One':1,
'Two':2,
'Three':3,
'Four':4,
'Five':5
}
def __init__(self,parent):
self.parent=parent
def __repr__(self):
return f'<Book {self.Name} {self.Price}, {self.Rating} stars>'
@property
def Name(self):
locator=Book_locator.NAME
title=self.parent.select_one(locator)
title_tag=title.attrs['title']
return title_tag
@property
def Link(self):
locator=Book_locator.LINK
link=self.parent.select_one(locator)
link_tag=link.attrs['href']
return link_tag
@property
def Price(self):
locator=Book_locator.PRICE
price=self.parent.select_one(locator).string
#price_tag=price.attrs['class']
expression='£([0-9]+\.[0-9]+)'
evaluate=re.search(price,expression)
return float(evaluate.group(1))
@property
def Rating(self):
locator=Book_locator.RATING
rating=self.parent.select_one(locator)
rating_tag=rating.attrs['class']
rating=[r for r in rating_tag if r != 'star-rating' ]
#rating1=filter(lambda x: x!='star-rating',rating_tag)
return Individual_book_detail.RATING.get(rating[0])
I am expecting book details from the website. But its showing
Process finished with exit code 0