Python 在单个

Python 在单个,python,beautifulsoup,Python,Beautifulsoup,我正在尝试使用beautifulsoup在这个HTML div中创建第一个和第二个标记-130和+110,如下所示: 但是我不知道如何刮第二个标签,只能刮第一个。多谢各位 from urllib.request import urlopen as uReq from bs4 import BeautifulSoup as soup day = "09" month = "10" year = "2017" my_url = 'https://www.sportsbookreview.com/

我正在尝试使用beautifulsoup在这个HTML div中创建第一个和第二个标记-130和+110,如下所示:

但是我不知道如何刮第二个标签,只能刮第一个。多谢各位

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

day = "09"
month = "10"
year = "2017"
my_url = 'https://www.sportsbookreview.com/betting-odds/mlb-baseball/?date=' + year + month + day

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

# html parser
page_soup = soup(page_html, "html.parser")

allBovadaOdds = page_soup.find_all("div", {"rel": "999996"})

firstOdds = allBovadaOdds[1].b.string
print(firstOdds)
您可以尝试使用soup。选择filter tags并将其用于范围内的i:以获取所有第二个标记。请注意,步长范围应为2

您可以尝试使用soup。选择filter tags并将其用于范围内的i:以获取所有第二个标记。请注意,步长范围应为2


我认为,你想要的东西可以写得相当简单

>>> import bs4
>>> import requests
>>> page = requests.get('https://www.sportsbookreview.com/betting-odds/mlb-baseball/?date=20171009').text
>>> soup = bs4.BeautifulSoup(page, 'lxml')
>>> soup.select('#eventLine-3330496-43 b')
[<b>-130</b>, <b>+110</b>]
>>> for item in soup.select('#eventLine-3330496-43 b'):
...     item.text
...     
'-130'
'+110'
然而,我注意到两个潜在的问题:

元素(即div的id等)的标签可能因web页面调用的不同而有所不同。 实际上有两列包含这对值。例如,通过使用预订代理和编号来确定所需物品可能更安全。
我认为,你想要的东西可以写得相当简单

>>> import bs4
>>> import requests
>>> page = requests.get('https://www.sportsbookreview.com/betting-odds/mlb-baseball/?date=20171009').text
>>> soup = bs4.BeautifulSoup(page, 'lxml')
>>> soup.select('#eventLine-3330496-43 b')
[<b>-130</b>, <b>+110</b>]
>>> for item in soup.select('#eventLine-3330496-43 b'):
...     item.text
...     
'-130'
'+110'
然而,我注意到两个潜在的问题:

元素(即div的id等)的标签可能因web页面调用的不同而有所不同。 实际上有两列包含这对值。例如,通过使用预订代理和编号来确定所需物品可能更安全。
我是你的朋友//div/b[2]将在每个div中找到第二个b//div[@rel=999996]/b[2]将只在具有给定值的rel属性的div中找到第二个b。如果要查找元素而不是属性,请删除@。@charlesduff,可能我在这里遗漏了一些内容,但是XPath不能与BeautifulSoup一起使用。@KeyurPotdar,BeautifulSoup的现代版本默认为lxml后端,它非常支持XPath。你是说lxml.etree吗?请注意答案上的日期-这是真的,因为BS3.XPath是你的朋友//div/b[2]将在每个div中找到第二个b//div[@rel=999996]/b[2]将只在具有给定值的rel属性的div中找到第二个b。如果要查找元素而不是属性,请删除@。@charlesduff,可能我在这里遗漏了一些内容,但是XPath不能与BeautifulSoup一起使用。@KeyurPotdar,BeautifulSoup的现代版本默认为lxml后端,它非常支持XPath。你是说lxml.etree吗?请注意答案上的日期-这对于BS3是正确的。