我试图在这个API的输出中导航到响应中的一个标记。但是,在尝试使用标准方法导航到标记之后,我得到了一个空响应。
from bs4 import BeautifulSoup
import urllib.request
import gzip
import io
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.5',
}
url = 'https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&q=' + 'AKIAJQVBDUUDGLXOEKYA' + '&site=stackoverflow'
req = urllib.request.Request(url, headers=headers)
response = urllib.request.urlopen(req)
time.sleep(3)
if response.info().get('Content-Encoding') == 'gzip':
pagedata = gzip.decompress(response.read())
elif response.info().get('Content-Encoding') == 'deflate':
pagedata = response.read()
elif response.info().get('Content-Encoding'):
print('Encoding type unknown')
else:
pagedata = response.read()
soup = BeautifulSoup(pagedata, "lxml")
print(soup)汤的产出:
<html><body><p>{"items":[{"tags":["c#","aws-lambda","aws-serverless"],"owner":{"reputation":188,"user_id":1395211,"user_type":"registered","accept_rate":62,"profile_image":"https://i.stack.imgur.com/WylN7.png?s=128&g=1","display_name":"Mostafa Fallah","link":"https://stackoverflow.com/users/1395211/mostafa-fallah"},"is_answered":true,"view_count":40,"accepted_answer_id":54550236,"answer_count":1,"score":2,"last_activity_date":1549445444,"creation_date":1540222981,"question_id":52933098,"link":"https://stackoverflow.com/questions/52933098/deploying-aws-serverless-lambda-application-with-amazonserverlessapplicationrepo","title":"Deploying AWS Serverless lambda Application with AmazonServerlessApplicationRepositoryClient does not work?"}],"has_more":false,"quota_max":300,"quota_remaining":275}</p></body></html>这就是我以前导航的地方:
tags = soup.find_all('p')
t = tags[0]
print(type(t))
print(t.attrs)但是,这将返回空的dict {},尽管我可以在标记中看到一些东西。不知道我做得对不对。谢谢你提前提供帮助。
发布于 2019-04-29 19:50:31
json格式的项,以便您可以转储和循环这些项。
import requests
url = 'https://api.stackexchange.com/2.2/search/advanced?order=desc&sort=activity&q=' + 'AKIAJQVBDUUDGLXOEKYA' + '&site=stackoverflow'
s=requests.get(url).json()
data = [(item['tags'],item['owner'],item['title']) for item in s['items']]
print(data)输出:
[([python', beautifulsoup'], {user_id': 7309225, profile_image': https://graph.facebook.com/10207802462833592/picture?type=large', user_type': registered', reputation': 532, link': https://stackoverflow.com/users/7309225/digvijay-sawant', accept_rate': 100, display_name': Digvijay Sawant'}, Beautiful Soup BS4 tag navigation'), ([c#', aws-lambda', aws-serverless'], {user_id': 1395211, profile_image': https://i.stack.imgur.com/WylN7.png?s=128&g=1', user_type': registered', reputation': 188, link': https://stackoverflow.com/users/1395211/mostafa-fallah', accept_rate': 62, display_name': Mostafa Fallah'}, Deploying AWS Serverless lambda Application with AmazonServerlessApplicationRepositoryClient does not work?')]发布于 2019-04-29 19:03:39
试试这个:
print(t.contents)t.attrs返回该标记的属性字典。
t.contents返回标记的内容(开始标记和结束标记之间的内容)。
发布于 2019-04-29 19:42:49
根据我上面的评论,<p>标记没有属性。您所看到的标记在标签之外:<p>…</p>。属性将在标记中,如:<p class="class_name" color="red">…</p>。要获取标记中的信息,请使用:
t = soup.p.string更新:与您已经启动的内容保持一致,您可以使用json模块获得“字典样式”输出,如下所示:
import json
t_dict = json.loads(t)
t_dict # this will output the json format data希望这能有所帮助。
https://stackoverflow.com/questions/55909090
复制相似问题