Python脚本-Findall

Python脚本-Findall,python,beautifulsoup,Python,Beautifulsoup,以下脚本列出了的所有标记的输出 但是,我想在标记中显示类id值。怎么办 输出如下所示: [<ar-save-item class="favorite" data-id="73715" data-imageurl="'http://images.media-allrecipes.com/userphotos/250x250/00/42/82/428269.jpg'" data-name='"Paneer"' data-type="'Recipe'"></ar-save-item&

以下脚本列出了
的所有标记的输出

但是,我想在标记中显示类id值。怎么办

输出如下所示:

[<ar-save-item class="favorite" data-id="73715" data-imageurl="'http://images.media-allrecipes.com/userphotos/250x250/00/42/82/428269.jpg'" data-name='"Paneer"' data-type="'Recipe'"></ar-save-item>, <ar-save-item class="favorite" data-id="212521" data-imageurl="'http://images.media-allrecipes.com/userphotos/250x250/00/32/99/329922.jpg'" data-name='"Shahi Paneer"' data-type="'Recipe'"></ar-save-item>, <ar-save-item class="favorite" data-id="221826" data-imageurl="'http://images.media-allrecipes.com/userphotos/250x250/01/03/63/1036376.jpg'" data-name='"Palak Paneer (Indian Spinach and Paneer)"' data-type="'Recipe'"></ar-save-item>

诸如此类。请帮助。

res
是一个
dict
。您可以通过
res['data-id']
或使用
get()
方法作为
res.get('data-id')
来获取值。如果没有
数据id
属性,最好使用
get()
,因为它返回
None
,但使用
数据id
作为
res
中的键会引发异常

import requests
from bs4 import BeautifulSoup

def getrec():

    key = "Paneer"
    url = "http://allrecipes.com/search/results/?wt="+key+"&sort=re"

    response = requests.get(url)
    result_page = BeautifulSoup(response.content,'lxml')
    r = result_page.find_all('ar-save-item')

    for res in r:
        print('data-id =', res.get('data-id'))

getrec()
输出

data-id = 73715
data-id = 212521
data-id = 221826
data-id = 212756
data-id = 232201
data-id = 222787
data-id = 232203
data-id = 240652
data-id = 138127
data-id = 256164
data-id = 221828
data-id = 212814
data-id = 106159
data-id = 159147
data-id = 86602
data-id = 237491
data-id = 213235
data-id = 228957
data-id = 228899
data-id = 232202

res
是一个
dict
。您可以通过
res['data-id']
或使用
get()
方法作为
res.get('data-id')
来获取值。如果没有
数据id
属性,最好使用
get()
,因为它返回
None
,但使用
数据id
作为
res
中的键会引发异常

import requests
from bs4 import BeautifulSoup

def getrec():

    key = "Paneer"
    url = "http://allrecipes.com/search/results/?wt="+key+"&sort=re"

    response = requests.get(url)
    result_page = BeautifulSoup(response.content,'lxml')
    r = result_page.find_all('ar-save-item')

    for res in r:
        print('data-id =', res.get('data-id'))

getrec()
输出

data-id = 73715
data-id = 212521
data-id = 221826
data-id = 212756
data-id = 232201
data-id = 222787
data-id = 232203
data-id = 240652
data-id = 138127
data-id = 256164
data-id = 221828
data-id = 212814
data-id = 106159
data-id = 159147
data-id = 86602
data-id = 237491
data-id = 213235
data-id = 228957
data-id = 228899
data-id = 232202

错误:ResultSet对象没有属性“get”。因为您正在执行
r.get()
。使用
res.get()
。仔细检查代码。您的代码以数据id=无的形式给出输出。我正在使用python 3.6正确复制代码。我在这里得到了正确的输出。从bs4导入请求import beautifulsou def getrec():key=“Paneer”url=”“print(url)response=requests.get(url)result_page=beautifulsou(response.content,'lxml')r=result_page.find_all('ar-save-item')在r:print('data-id=',res get中为res打印(r)('data-id'))getrec()错误:ResultSet对象没有属性“get”。因为您正在执行
r.get()
。请使用
res.get()
。仔细检查代码。您的代码以数据id=None的形式给出输出。我正在使用python 3.6正确复制代码。我在这里得到正确的输出。从bs4导入请求导入BeautifulSoup def getrec():key=“Paneer”url=”“print(url)response=requests。获取(url)结果\页面=BeautifulSoup(response.content,'lxml')r=result\u page.find\u all('ar-save-item')r中res的打印(r):print('data-id=',res.get('data-id'))getrec()