신종원*_*신종원 1 python web-crawler python-3.x
我尝试做的是抓取每个二手车库存数据列表,如果在“价格”标签中每个数据的第 4 列(粉红色图像表示“售罄”)有“图像”数据,我将跳过该列表并继续抓取下一个股票数据。
(我上面的意思是跳过整个以下代码并开始下一轮“for循环”。“continue”跳过唯一的“if”函数并继续运行以下代码。)
下面是我的代码
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])
Run Code Online (Sandbox Code Playgroud)
您正在寻找continue. 它完全符合您的要求。
例如,打印不会对成对运行。继续跳到下一次迭代:
for i in range(5):
if i % 2 == 0:
continue
print(i)
# Do not print evens
1
3
Run Code Online (Sandbox Code Playgroud)
这个问题也很有帮助!