Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.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中使用BeautifulSoup提取id以特定字符串开头的元素_Python_Beautifulsoup - Fatal编程技术网

在python中使用BeautifulSoup提取id以特定字符串开头的元素

在python中使用BeautifulSoup提取id以特定字符串开头的元素,python,beautifulsoup,Python,Beautifulsoup,我正在尝试用BS4做一些网页抓取 到目前为止,我已经提取了 我尝试了item.id,但不起作用 我缺少什么?将re模块与id一起使用 以下是方法: 从bs4导入美化组 进口稀土 如果名称=“\uuuuu main\uuuuuuuu”: html=“” soup=BeautifulSoup(html,'html.parser') 打印(soup.find('a',id=re.compile('^entry')) 输出: <a href="http://www.sampleurl.com/

我正在尝试用BS4做一些网页抓取

到目前为止,我已经提取了

我尝试了
item.id
,但不起作用


我缺少什么?

re
模块与
id
一起使用
以下是方法:

从bs4导入美化组
进口稀土
如果名称=“\uuuuu main\uuuuuuuu”:
html=“”
soup=BeautifulSoup(html,'html.parser')
打印(soup.find('a',id=re.compile('^entry'))
输出:

<a href="http://www.sampleurl.com/static/welcome" id="entry_1">Lamborghini </a>


item.get('id')
?是的,如果条件是“id以'entry'开头”
url=[汤中的项目。如果item.get('id',”)[:6]==“entry_”]]
现在你有两个问题,但我认为一次性解决方案是可以的。此外,这也是选择节点的官方/推荐方式
<a href="http://www.sampleurl.com/static/welcome" id="entry_1">Lamborghini </a>
<a href="http://www.sampleurl.com/static/welcome" id="entry_1">Lamborghini </a>