3

I have developing a web-crawler for this web-address, and just got a problem.

What I try to do is to crawl each list of used-car stock data, and if there is an "image" data at 4th column of each data (which is pink image meaning "sold-out") in "price" tag, I will skip that list and continue to crawl next stock data.

(What I mean above is to skip the entire following code and start the next round of "for loop". The "continue" skips the only "if" function and keeps running the following code.)

Below is my code

from bs4 import BeautifulSoup
import urllib.request

URL=http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I&page=20
res = urllib.request.urlopen(URL)
html = res.read()
soup = BeautifulSoup(html, 'html.parser')
table = soup.find('table', class_='cyber')

# 50 lists per each page
links = []
for p in range(50):

    #Car_Price
    car_price=table.find_all('td', class_='price')
    if car_price[p].find('em').text:
        car_price_confirm = car_price[p].find('em').text
    elif car_price[p].find('em').find('img'):
        pass

    carinfo = table.find_all('td', class_='carinfo')
    carinfo_title = carinfo[p].find('a', class_='title').text
    links.append(carinfo[p].find('a')['href'])

    print(p+1, car_price_confirm, link[p])
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
신종원
  • 195
  • 1
  • 11
  • Please don't change your questions completely after you've received answers. Ask another question if the need be. – Bhargav Rao Dec 27 '16 at 12:33

2 Answers2

9

You are looking for continue. It does exactly what you want.

An example, the print does not run for the pairs. Continue to jump to the next iteration:

for i in range(5):
    if i % 2 == 0:
        continue
    print(i)

# Do not print evens
1
3

This question can also be very helpful!

Community
  • 1
  • 1
Lucas
  • 6,869
  • 5
  • 29
  • 44
  • Thanks for reply. But what I meant is to skip the entire following code and start the next round of "for loop". The "continue" skips the only "if" function and keeps running the following code. – 신종원 Dec 27 '16 at 07:32
  • Thanks for the comment, I think I figured out the skip and re-start "for loop" problem. But I got another problem from that code. Would you mind help me to work this out? – 신종원 Dec 27 '16 at 08:09
  • You TOTALLY changed the question, I do not think this is right. At least you tried to find out what the mistake is? – Lucas Dec 27 '16 at 08:22
1

EDIT: continue is skipping the whole iteration. It has no effect on if statements. Check your code.

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.

To skip the rest of the current for-loop use the continue statement.

for p in range(50):
    car_price=table.find_all('td', class_='price')

    if car_price[p].find('em').find('img'):
        continue

    ...
pixelarbeit
  • 484
  • 2
  • 11
  • Thanks for the comment, I think I figured out the skip and re-start "for loop" problem. But I got another problem from that code. Would you mind help me to work this out? – 신종원 Dec 27 '16 at 08:09