如何从元素<p>获取值?

时间:2019-06-08 11:47:15

标签: python html web-scraping beautifulsoup

这是网页和我的python代码的摘要。我正在尝试打印$ 2.00的值。

我的代码为我提供了必需的HTML元素输出,但没有值$ 2.00或07/06。为什么?

<div class="io_col1_left">
  <p data-quoteapi="price" class="quoteapi-number quoteapi-price">$2.00</p>
  <p class="io_data" data-quoteapi="dateTime">Closed - 07/06</p>
</div>

from bs4 import BeautifulSoup
import re
import urllib2
import time
import requests
url = 'https://www.localhost/test'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}

response = requests.get(url, headers=headers)
print response

soup = BeautifulSoup(response.text, 'html.parser')

soup.findAll('div', class_='io_col1_left')

3 个答案:

答案 0 :(得分:0)

strip()是删除字符串开头和结尾的空格。

替换您的代码:

soup.findAll('div', class_='io_col1_left')

收件人:

div = soup.find('div', {'class':'io_col1_left'})

price = div.find("p",{'class':'quoteapi-price'})

dateTime = div.find("p",{'class':'io_data'})

print(price.text.strip())
print(dateTime.text.strip())

O / P:

$2.00
Closed - 07/06

答案 1 :(得分:0)

页面动态加载内容。您可以在“网络”标签中找到API来源并使用它

import requests

headers = {'User-Agent' : 'Mozilla/5.0',
           'Accept' : 'application/json',
           'Referer' : 'https://www.marketindex.com.au/asx/eof'}

r = requests.get('https://quoteapi.com/api/v5/symbols/eof.asx?appID=af5f4d73c1a54a33&averages=1&liveness=delayed', headers = headers).json()
price = r['quote']['price']
time =  r['quote']['time']
print(price, time)

答案 2 :(得分:0)

如果您只想废弃$2.00,则建议您替换:

soup.findAll('div', class_='io_col1_left')

具有:

soup.find('div', class_='io_col1_left').findNext("p").getText()

如果您要抓取包含多个div的整个表,我建议将其替换为该行:

table_divs = soup.findAll('div', class_='io_col1_left')
for x in table_divs:
    print(x.findNext("p").getText())

我希望这对您有帮助