Python 如何在刮网时继续循环

Python 如何在刮网时继续循环,python,python-3.x,web-scraping,beautifulsoup,Python,Python 3.x,Web Scraping,Beautifulsoup,我编写了一个简单的脚本,以探索如何使用python进行web抓取。我选择了这个网址: 页面中有48个项目,每个项目都有品牌、风格等详细信息,除了第16个项目外,我的代码在第16个项目时停止。所以我的问题是我如何继续这个循环,或者我如何说传递这些细节。下面是我的代码 from urllib.request import urlopen as uReq from bs4 import BeautifulSoup as soup my_url = 'https://www.ebay.co.

我编写了一个简单的脚本,以探索如何使用python进行web抓取。我选择了这个网址:

页面中有48个项目,每个项目都有品牌、风格等详细信息,除了第16个项目外,我的代码在第16个项目时停止。所以我的问题是我如何继续这个循环,或者我如何说传递这些细节。下面是我的代码

    from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup


my_url = 'https://www.ebay.co.uk/b/Mens-Coats-Jackets/57988/bn_692010'

#opening up connection, grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

# html parsing
page_soup = soup(page_html, 'html.parser')

#grabs each product
containers = page_soup.findAll('div',{'class':'s-item__wrapper clearfix'})

filename = 'ebayproducts1.csv'
f = open(filename, 'w+')

headers = 'product_name, item_price, item_style, shipping_detail\n'

f.write(headers)

contain = containers[0]
container = containers[0]

for container in containers:
    product_name = container.h3.text

    item_details_container = container.findAll('div',{'class':'s-item__details clearfix'})
    item_price = item_details_container[0].div.text

    item_style = item_details_container[0].findAll('span',{'class':'s-item__detail s-item__detail--secondary'})[0].text

    shipping_detail = item_details_container[0].findAll('span',{'class':'s-item__shipping s-item__logisticsCost'})[0].text


    print('product_name: '+ product_name)

    print('item_price: ' + item_price)

    print('item_style: ' + item_style)

    print('shipping_detail: ' + shipping_detail)

    f.write("%s,%s,%s,%s\n" %( product_name, item_price, item_style, shipping_detail))

您可能在
容器
列表中遇到与正在搜索的所有其他元素不同的元素或标记

通过在
soup.findAll()
方法中更改搜索参数,可以更改指定容器列表的方式

试着打印
容器
,找出该列表中第16项不同的原因,并相应地调整搜索

或者,您可以使用一个try-except,如下所示:

for container in containers:
    try:
       product_name = container.h3.text
       item_details_container = container.findAll('div',{'class':'s-item__details clearfix'})
       item_price = item_details_container[0].div.text
       item_style = item_details_container[0].findAll('span',{'class':'s-item__detail s- 
       item__detail--secondary'})[0].text
       shipping_detail = item_details_container[0].findAll('span',{'class':'s-item__shipping s-item__logisticsCost'})[0].text

       # etc ...

    except <name of your error here, eg. TypeError>:
        print(f'except triggered for {container}')

对于容器中的容器:
尝试:
产品名称=container.h3.text
item\u details\u container=container.findAll('div',{'class':'s-item\u details clearfix'})
item\u price=item\u details\u容器[0]。div.text
item_style=item_details_容器[0]。findAll('span',{'class':'s-item_details s-
项目详细信息--辅助'})[0]。文本
shipping_detail=item_details_容器[0]。findAll('span',{'class':'s-item_uuShipping s-item_uLogisticsCost'})[0]。文本
#等等。。。
除:
打印(f'除了为{container}'触发)

有些项目不存在是正确的,您不能在所有情况下单独测试此on位置或选择器,例如样式。您可以测试容器文本中是否存在样式。拥有更多Python知识的人可能会将其整理成更具Python风格和效率的东西

import requests
from bs4 import BeautifulSoup as bs
import re
import pandas as pd
pattern = re.compile(r'Style:')
url = 'https://www.ebay.co.uk/b/Mens-Coats-Jackets/57988/bn_692010?_pgn=1'
res = requests.get(url)
soup = bs(res.content, 'lxml')
results = []
for item in soup.select('.s-item'):
    x = item.select_one('.s-item__title')
    title = x.text if x else None
    x = item.select_one('.s-item__price')
    price = x.text if x else None
    x = item.select_one('.s-item__shipping')
    shipping = x.text if x else None
    x = item.find('span', text=pattern)
    style = x.text.replace('Style: ','') if x else None
    results.append([title, price, shipping, style])

df = pd.DataFrame(results)
print(df)