【问题标题】:Select Price value only from td in python仅从 python 中的 td 中选择价格值
【发布时间】:2018-04-27 03:44:35
【问题描述】:

我试图从 html td 标签中仅捕获价格值,但问题是还有其他具有相同类名的 td:请参见下图。 enter image description here

这是我写的代码

from  builtins import any as b_any
from urllib.parse import urlparse
from urllib.parse import urljoin
from collections import Counter
import urllib.request
import csv
import schedule
import time
import re
from bs4 import BeautifulSoup

url="http://offer.ebay.es/ws/eBayISAPI.dll?ViewBidsLogin&item=122713288532&rt=nc&_trksid=p2047675.l2564"

req = urllib.request.Request(url, headers={'User-agent': 'Mozilla/5.0'})

htmlpage = urllib.request.urlopen(req)

html = htmlpage.read().decode('utf-8')

soup = BeautifulSoup(html,"html.parser")

table = soup.find_all('td',{'class':'onheadNav'})

'''for txt in table:
    nametxt = txt.text
    result = ''.join([i for i in nametxt if not i.isdigit()])
    cleantxt = result.replace('(','')
    print(cleantxt.replace(')',''))

    rank = txt.a.text
    print(rank)'''
price = soup.select('td.contentValueFont')
for pr in price:
    print(pr.text)

如果我在 for 循环中分割价格,它将只获得第一个价格,但我想一次获得所有价格。

编辑描述: 我想捕获所有价格,但问题是三个 td 具有相同的类名,一个 td 用于 Cantidad(数量)的价格,一个用于日期,这些都具有相同的类。当我尝试仅获取价格部分时,我的代码将返回所有三个 td。我希望你现在明白了

【问题讨论】:

  • 您的描述自相矛盾:仅捕获价格价值 我想一次获得所有价值。更新您的问题
  • 我想捕获所有价格,但问题是有三个具有相同类名的 td,一个 td 用于 Cantidad(数量)的价格,一个用于日期,这些都具有相同的类。当我尝试仅获取价格部分时,我的代码将返回所有三个 td。我希望你现在明白了。
  • 我不太了解beautifulsoup,但您可以尝试按位置获取td(例如td[1]),而不是按类别获取。
  • 我按照您的要求对它进行了切片,但它只返回第一个价格并打破循环。它不会进入下一次迭代。

标签: python html regex python-3.x beautifulsoup


【解决方案1】:

懒惰的方式:

soup = BeautifulSoup(html,"html.parser")

table = soup.find_all('table')

trs = table[9].select('tr') # You should select the table first (use your way)

for tr in trs: # loop the tr in the table
    if len(tr.select('td')) > 2: # check length
        print(tr.select('td')[2].text) # select third td

【讨论】:

    【解决方案2】:

    您需要做的是在表中找到您要抓取的所有“tr”标签,然后遍历所有这些标签以从特定的“td”中获取文本。

    类似这样的:

    table = soup.find_all('table')
    for tr in table[9].find_all('tr')[1:-1]:
        price = tr.find_all('td')[2].text.strip()
        print(price)
    

    经过一番研究,我们可以发现我们想要的表格是页面上的第 10 个表格,因此是table[9]。另外,由于我们不想要第一个和最后一个 'tr',所以我们需要 find_all('tr')[1:-1]

    希望这能解决您的问题。

    【讨论】:

      【解决方案3】:

      短解:

      from bs4 import BeautifulSoup
      import requests
      
      url = "http://offer.ebay.es/ws/eBayISAPI.dll?ViewBidsLogin&item=122713288532&rt=nc&_trksid=p2047675.l2564"
      html = requests.get(url).content
      soup = BeautifulSoup(html, "html.parser")
      
      prices =[ price.string.replace('\xa0', ' ')
                for price in soup.select('td.contentValueFont') if price.string.endswith('EUR')]
      print(prices)
      

      输出:

      ['4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '8,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR', '14,90 EUR', '4,90 EUR', '4,90 EUR', '4,90 EUR']
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-17
        • 1970-01-01
        相关资源
        最近更新 更多