Цикл массива URL для разбора HTML, не зацикливается - PullRequest
0 голосов
/ 18 апреля 2019

Я пытаюсь извлечь описание продукта, первый цикл проходит через каждый продукт, а вложенный цикл входит в каждую страницу продукта и получает описание для извлечения.

  for page in range(1, 2):
      guitarPage = 
  requests.get('https://www.guitarguitar.co.uk/guitars/acoustic/page- 
 {}'.format(page)).text
      soup = BeautifulSoup(guitarPage, 'lxml')
      guitars = soup.find_all(class_='col-xs-6 col-sm-4 col-md-4 col-lg-3')

это цикл для каждого продукта

for guitar in guitars:

    title_text = guitar.h3.text.strip()
    print('Guitar Name: ', title_text)
    price = guitar.find(class_='price bold small').text.strip()
    print('Guitar Price: ', price)

    priceSave = guitar.find('span', {'class': 'price save'})
    if priceSave is not None:
        priceOf = priceSave.text
        print(priceOf)
    else:
        print("No discount!")

    image = guitar.img.get('src')
    print('Guitar Image: ', image)

    productLink = guitar.find('a').get('href')
    linkProd = url + productLink
    print('Link of product', linkProd)

здесь я добавляю ссылки, собранные в массив

    productsPage.append(linkProd)

вот моя попытка зайти на каждую страницу продукта и извлечь описание

    for products in productsPage:
        response = requests.get(products)
        soup = BeautifulSoup(response.content, "lxml")
        productsDetails = soup.find("div", {"class":"description-preview"})
        if productsDetails is not None:
            description = productsDetails.text
            # print('product detail: ', description)
        else:
            print('none')
        time.sleep(0.2)

    if None not in(title_text,price,image,linkProd, description):
        products = {
            'title': title_text,
            'price': price,
            'discount': priceOf,
            'image': image,
            'link': linkProd,
            'description': description,

        }
        result.append(products)
        with open('datas.json', 'w') as outfile:
            json.dump(result, outfile, ensure_ascii=False, indent=4, separators=(',', ': '))
        # print(result)
        print('--------------------------')
    time.sleep(0.5)

Результат должен быть

{
        "title": "Yamaha NTX700 Electro Classical Guitar (Pre-Owned) #HIM041005",
        "price": "£399.00",
        "discount": null,
        "image": "https://images.guitarguitar.co.uk/cdn/large/150/PXP190415342158006-3115645f.jpg?h=190&w=120&mode=crop&bg=ffffff&quality=70&anchor=bottomcenter",
        "link": "https://www.guitarguitar.co.uk/product/pxp190415342158006-3115645--yamaha-ntx700-electro-classical-guitar-pre-owned-him",
        "description": "\nProduct Overview\nThe versatile, contemporary styled NTX line is designed with thinner bodies, narrower necks, 14th fret neck joints, and cutaway designs to provide greater comfort and playability f... read more\n"
    },

но описание работает для первого и не изменится позже.

[
    {
        "title": "Yamaha APX600FM Flame Maple Tobacco Sunburst",
        "price": "£239.00",
        "discount": "Save £160.00",
        "image": "https://images.guitarguitar.co.uk/cdn/large/150/190315340677008f.jpg?h=190&w=120&mode=crop&bg=ffffff&quality=70&anchor=bottomcenter",
        "link": "https://www.guitarguitar.co.uk/product/190315340677008--yamaha-apx600fm-flame-maple-tobacco-sunburst",
        "description": "\nProduct Overview\nOne of the world's best-selling acoustic-electric guitars, the APX600 series introduces an upgraded version with a flame maple top. APX's thinline body combines incredible comfort,... read more\n"
    },
    {
        "title": "Yamaha APX600FM Flame Maple Amber",
        "price": "£239.00",
        "discount": "Save £160.00",
        "image": "https://images.guitarguitar.co.uk/cdn/large/150/190315340676008f.jpg?h=190&w=120&mode=crop&bg=ffffff&quality=70&anchor=bottomcenter",
        "link": "https://www.guitarguitar.co.uk/product/190315340676008--yamaha-apx600fm-flame-maple-amber",
        "description": "\nProduct Overview\nOne of the world's best-selling acoustic-electric guitars, the APX600 series introduces an upgraded version with a flame maple top. APX's thinline body combines incredible comfort,... read more\n"
    },
    {
        "title": "Yamaha AC1R Acoustic Electric Concert Size Rosewood Back And Sides with SRT Pickup",
        "price": "£399.00",
        "discount": "Save £267.00",
        "image": "https://images.guitarguitar.co.uk/cdn/large/105/11012414211132.jpg?h=190&w=120&mode=crop&bg=ffffff&quality=70&anchor=bottomcenter",
        "link": "https://www.guitarguitar.co.uk/product/11012414211132--yamaha-ac1r-acoustic-electric-concert-size-rosewood-back-and-sid",
        "description": "\nProduct Overview\nOne of the world's best-selling acoustic-electric guitars, the APX600 series introduces an upgraded version with a flame maple top. APX's thinline body combines incredible comfort,... read more\n"
    }
]

это результат, который я получаю, он постоянно меняется, иногда показывает предыдущее описание продукта

1 Ответ

0 голосов
/ 18 апреля 2019

Это делает цикл, но кажется, что существуют защитные меры вместо серверной части, и страницы, которые терпят неудачу, изменяются. Страницы, которые потерпели неудачу, я проверил, и они искали содержимое Похоже, в моем тестировании не хватило одной меры (я не пробовал спать больше 2, но пробовал некоторые изменения IP и User-Agent со спящими <= 2.) </p>

Вы можете попробовать чередование IP-адресов и пользовательских агентов, отменить повторные попытки, изменить время между запросами.

Смена прокси: https://www.scrapehero.com/how-to-rotate-proxies-and-ip-addresses-using-python-3/

Изменение User-Agent: https://pypi.org/project/fake-useragent/

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