I have a question for Python Clawing.

Asked 2 years ago, Updated 2 years ago, 42 views

I touched Python for the first time because I wanted to crawl https://www.youtube.com/watch?v=aYdNm27HvLA I was copying this video. If you search for the product on SlickDeal, you can find the image, price, and link It's about colliding the results If you search for images in a video, all the images are on the search page If you search SlickDeal, there are empty images in between There is an index error.

Like this.

Code is currently

I made it like this.

The result I want to do is search and if there is no image, just move on to the next line...

Please answer...

crawling

2022-09-21 12:39

1 Answers

The above problem occurs because the list of images is empty.

You can skip if the list of images is empty with the following conditional expression:

if image.select("img.lazyimg") == []:
    continue

The full code to which the conditional expression was applied is as follows.

import requests
from bs4 import BeautifulSoup

keyword = "apple ipad"
url = "https://slickdeals.net/newsearch.php?src=SearchBarV2&q={}&searcharea=deals&searchin=first".format(keyword)
r = requests.get(url)
bs = BeautifulSoup(r.content, "lxml")
divs = bs.select("div.resultRow")

for d in divs:

    image = d.select("div.dealImg > a")[0]

    if image.select("img.lazyimg") == []: # Added
        continue

    rimages = image.select("img.lazyimg")[0]
    image1 = rimages.get("data-original")
    if rimages is None:
        continue
    print(image1)


2022-09-21 12:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.