Как получить все списки URL с главной страницы с помощью python веб-очистки - PullRequest
0 голосов
/ 23 марта 2020

Я написал код для очистки веб-страниц, Мой код в порядке, кроме двух вопросов. Со страницы сведений все в порядке, просто ISBN НЕТ, а с главной страницы мне нужны все URL-адреса листинга, чтобы мой код мог вычищать даты из списков. Пожалуйста, объясните мне, как я могу решить эту проблему. Оба URL-адреса (главная страница и страница сведений) находятся в коде. Спасибо!

вот мой код:

import requests
from bs4 import BeautifulSoup
import csv

def get_page(url):
    response = requests.get(url)

    if not response.ok:
        print('server responded:', response.status_code)
    else:
        soup = BeautifulSoup(response.text, 'html.parser') # 1. html , 2. parser
    return soup

def get_detail_data(soup):

    try:
        title = soup.find('span',class_="title product-field",id=False).text
    except:
        title = 'empty'  
    print(title)
    try:
        writer = soup.find('a',class_="contributor-name",id=False).text
    except:
        writer = 'empty'  
    print(writer)   
    try:
        original_price = soup.find('div',class_="original-price",id=False).find('span').text
    except:
        original_price = 'empty'  
    print(original_price)  
    try:
        active_price = soup.find('div',class_="active-price",id=False).find('span').text
    except:
        active_price = 'empty'  
    print(active_price)     
    try:
        img = soup.find('div',class_="image-actions image-container product-type-icon-container book",id=False).find('img').attrs['src']
    except:
        img = 'empty'  
    print(img)   
    try:
        isbn = soup.find('div',class_="bookitem-secondary-metadata",id=False).find('li').attrs['ISBN: ']
    except:
        isbn = 'empty'  
    print(isbn) 
    data = {
        'title'             :   title,
        'writer'            :   writer,
        'original_price'    :   original_price,
        'active_price'      :   active_price,
        'image'             :   img,
        'isbn'              :   isbn
    }
    return data

def get_index_data(soup):
    titles_link = soup.find_all('a',class_="body_link_11")
    try:
        inks = soup.find('div', class_="item-info",id=False).find('p').find('a').get('href')
    except:
        inks = "empty"
    print(inks)

def main():
    #detail_page_url = "https://www.kobo.com/ww/en/ebook/mum-dad-1"
    mainurl = "https://www.kobo.com/ww/en/list/new-hot-in-fiction/youL53408U25RHrVu3wR5Q"
    #get_page(url)
    #get_detail_data(get_page(detail_page_url))
    get_index_data(get_page(mainurl))
if __name__ == '__main__':
    main()

1 Ответ

3 голосов
/ 23 марта 2020
import requests
import re
import json
from bs4 import BeautifulSoup
import csv


def Soup(content):
    soup = BeautifulSoup(content, 'html.parser')
    return soup


def Main(url):
    r = requests.get(url)
    soup = Soup(r.content)
    scripts = soup.findAll("script", type="application/ld+json",
                           text=re.compile("data"))
    prices = [span.text for span in soup.select(
        "p.product-field.price span span") if span.text != "USD"]
    with open("data.csv", 'w', newline="") as f:
        writer = csv.writer(f)
        writer.writerow(["Title", "Writer", "Price", "ISBN", "IMG", "URL"])
        for script, price in zip(scripts, prices):
            script = json.loads(script.text)
            title = script["data"]["name"]
            author = script["data"]["author"][0]["name"]
            img = f'https:{script["data"]["thumbnailUrl"]}'
            isbn = script["data"]["isbn"]
            url = script["data"]["url"]
            writer.writerow([title, author, price, isbn, img, url])


Main("https://www.kobo.com/ww/en/list/new-hot-in-fiction/youL53408U25RHrVu3wR5Q")

Выход: Просмотр онлайн

Образец вывода:

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...