[英]Beautiful Soup Grabbing next element
I am trying to pull the election electoral votes to check when it updates. 我试图拉动选举人选票,以检查其更新情况。 But the hard part is that all the classes change on every refresh. 但困难的是,所有类在每次刷新时都会更改。 I want to search for the text Trump
and then find the next element which is the count. 我想搜索Trump
文本,然后找到下一个元素即计数。
I can find the element, by searching for the string Trump
: 我可以通过搜索字符串Trump
找到元素:
import requests
import re
from bs4 import BeautifulSoup
url = "https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=who+is+winning+the+presidential+election&eob=enn/p//1/0///////////"
r = requests.get(url)
soup = BeautifulSoup(r.content)
elm = soup.find(text='Trump')
print elm.text
I found the Trump element, with lm = soup.find(text='Trump')
, but I don't know how to grab the next element after that one. 我找到了Trump元素,并带有lm = soup.find(text='Trump')
,但是我不知道如何获取该元素之后的下一个元素。
Your current code is looking for an exact match of a node with that text. 您当前的代码正在寻找与该文本完全匹配的节点。 Try this: 尝试这个:
soup.body.findAll(text=re.compile('Trump'))
> ["Donald Trump is US president-elect in 'America's Brexit' as Hillary Clinton concedes election - live", 'Donald Trump ', 'Donald Trump wins presidential election, plunging US into uncertain future'... ]
You'll instead be looking for a regular expression containing the target text. 您将改为寻找包含目标文本的正则表达式。 You can refine the regular expression you're looking for, for example: 您可以优化要查找的正则表达式,例如:
b.body.findAll(text=re.compile('Trump wins .+? uncertain future'))
> ['Donald Trump wins presidential election, plunging US into uncertain future']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.