Python和BS4:从某个div开始搜索术语(Python & BS4: Start searching for term starting at certain div)

编程入门 行业动态 更新时间:2024-10-24 14:21:06
Python和BS4:从某个div开始搜索术语(Python & BS4: Start searching for term starting at certain div)

Python 2.7.6 + BeautifulSoup 4 +在这里请求noob。

我的问题是关于搜索div类的内容,比如在这个网站上 。 我只想在每列包含信息时使用行的内容。 我能够编写一段代码来提取fuelprice的div级内容(在网站上是第1列)。 有时首先列出的加油站是关闭的,没有价格出现。 所以我的代码抓住了实际包含价格的第一个div。

pricediv = soup.find("div", {"class": "price"}) price = pricediv.text

接下来,我想获取我从中提取价格的加油站的名称和地址,这些加油站包含在另外两个div类中。 我该怎么做

location = soup.find("div", {"class": "location_name"})

开始搜索包含我之前提取的汽油价格的div级的位置? 否则,如果前两个加油站关闭,我的可变价格将包含第三个加油站的汽油价格。 但是如果我运行代码来找到位置(如上所述),它将返回第一个位置(封闭的加油站号1)。 所以我希望它开始在price-div之后寻找位置div。

我希望我明确说出我要找的东西,有人可能会暗示我。 提前致谢!

Python 2.7.6 + BeautifulSoup 4 + requests noob here.

My question concerns searching through contents of div-classes, i.e. like on this site. I only want to use the content of a line when each column contains information. I was able to write a piece of code that extracts the content of the div-class of the fuelprice (which in on the website is column 1). Sometimes the gas stations that are listed first are closed and no price appears. So my code grabs the first div that contains a price actually.

pricediv = soup.find("div", {"class": "price"}) price = pricediv.text

Next, I want to grab the name and address of the gas station I extracted the price from, which are contained in two further div classes. How can I make

location = soup.find("div", {"class": "location_name"})

start searching at the position of the div-class that contained the gas price I extracted earlier? Otherwise, if i.e. the first two gas stations are closed, my variable pricediv will contain the gas price of the third gas station. But if I run the code to find the location (as above), it will return the very first location (of the closed gas station number 1). So I want it to start looking for the location div right after the price-div.

I hope I made clear what I am looking for and that somebody may have a hint for me. Thanks in advance!

最满意答案

从你提供的链接,你的价格 div是priceblock div的子节点 ,它同样是price_entry_table div的子节点 ,所以为了找到你想要的div ,你需要使用parent ,它应该是这样的:

pricediv = soup.find('div', {'class': 'price'}) price = pricediv.text # use parent.parent to get to the price_entry_table div, then find location_name locationdiv = pricediv.parent.parent.find('div', {'class': 'location_name'}) location = locationdiv.text print price, location # sample result 1.379 Tankstelle Wagner/DBV Würzburg

此外,如果你需要访问所有div ,你可能想使用像@PadraicCunningham建议的findAll ,如下所示:

for pricediv in soup.findAll('div', {'class': 'price'}): price = pricediv.text ... do your remaining code here ...

From the link you provided, your price div is a child of priceblock div which again is a child of price_entry_table div, so in order to locate the div you want, you need to use parent, this is what it should look like:

pricediv = soup.find('div', {'class': 'price'}) price = pricediv.text # use parent.parent to get to the price_entry_table div, then find location_name locationdiv = pricediv.parent.parent.find('div', {'class': 'location_name'}) location = locationdiv.text print price, location # sample result 1.379 Tankstelle Wagner/DBV Würzburg

Also, if you need to access all divs, you may want to use findAll like @PadraicCunningham advised, something like this:

for pricediv in soup.findAll('div', {'class': 'price'}): price = pricediv.text ... do your remaining code here ...

更多推荐

本文发布于:2023-07-29 20:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:术语   div   Python   starting   term

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!