Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用beatifoulsoup获得第二个标签_Python - Fatal编程技术网

Python 用beatifoulsoup获得第二个标签

Python 用beatifoulsoup获得第二个标签,python,Python,我开始做一些网站清理项目,在同一个父标签中选择第二个标签时遇到了一些困难。我试过用谷歌,但还是不太明白 我的代码如下所示: url = 'url to site' content = requests.get(url).text soup = BeautifulSoup(content, 'lxml') car_add = soup.find('div', class_='offer-wrapper') ad_title = car_add.find('h3', class_='lheigh

我开始做一些网站清理项目,在同一个父标签中选择第二个标签时遇到了一些困难。我试过用谷歌,但还是不太明白

我的代码如下所示:

url = 'url to site'
content = requests.get(url).text
soup = BeautifulSoup(content, 'lxml')

car_add = soup.find('div', class_='offer-wrapper')

ad_title = car_add.find('h3', class_='lheight22 margintop5').a.strong.text
ad_price = car_add.find('p', class_='price').text
ad_location = car_add.find('td', class_='bottom-cell').div.p.small.span.text
ad_time_and_location = car_add.find('td', class_='bottom-cell').div.p
print(ad_time_and_location.prettify())
这将打印出以下内容:

<p class="lheight16">
 <small class="breadcrumb x-normal">
  <span>
   <i data-icon="location-filled">
   </i>
   Otopeni
  </span>
 </small>
 <small class="breadcrumb x-normal">
  <span>
   <i data-icon="clock">
   </i>
    09:25
  </span>
 </small>
</p>
然后它会自动默认为第一个文本标记

我尝试过使用select()方法,但它给了我一个空列表。有人能帮我吗

谢谢大家!

您可以使用
find_all('span')
获取包含所有
span
的列表,然后使用
[1]
从列表中获取第二个元素

from bs4 import BeautifulSoup as BS

text = '''<p class="lheight16">
 <small class="breadcrumb x-normal">
  <span>
   <i data-icon="location-filled">
   </i>
   Otopeni
  </span>
 </small>
 <small class="breadcrumb x-normal">
  <span>
   <i data-icon="clock">
   </i>
    09:25
  </span>
 </small>
</p>'''

soup = BS(text, 'html.parser')

item = soup.find('p').find_all('span')[1].get_text(strip=True)

print(item)
从bs4导入美化组作为BS
text=''

耳鸣 09:25

'' soup=BS(文本“html.parser”) item=soup.find('p')。find_all('span')[1]。get_text(strip=True) 打印(项目)
请提供一个MCVE示例。由于我们没有您所有的输入HTML,请跳过请求代码,只给我们复制此内容所需的HTML片段。您可以使用
find_all('span')
获取包含所有
span
的列表,然后使用
[1]
从list.lxml获取第二个元素。xpath语法更好,您可以使用下标[1]直接在表达式中,
.xpath
解决方案是@smci,谢谢你的提示。我对网络垃圾真的是个新手,我刚刚开始学习这些解析器。再次感谢!非常感谢你!结果很好。祝你今天愉快:)@GhirasimDaniel你可以把答案标记为已接受。当你有足够的声誉时,你可以投票给它。我刚刚做了标记,昨天就忘了。谢谢你的提醒!:)
from bs4 import BeautifulSoup as BS

text = '''<p class="lheight16">
 <small class="breadcrumb x-normal">
  <span>
   <i data-icon="location-filled">
   </i>
   Otopeni
  </span>
 </small>
 <small class="breadcrumb x-normal">
  <span>
   <i data-icon="clock">
   </i>
    09:25
  </span>
 </small>
</p>'''

soup = BS(text, 'html.parser')

item = soup.find('p').find_all('span')[1].get_text(strip=True)

print(item)