gpt4 book ai didi

python - 使用 Mechanize 和 Python Django View 解析 JSON 输出

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:06 25 4
gpt4 key购买 nike

我目前正在使用 Python 和 Mechanize 在 BING 中进行网站搜索,例如:site:somedomain.com

它向 bing 提交正常并返回输出 - 看起来像 Json?我似乎无法找到进一步解析结果的好方法。 是 JSON 吗?

我得到的输出如下:

Link(base_url=u'http://www.bing.com/search?q=site%3Asomesite.com', url='http://www.somesite.com/prof.php?pID=478', text='SomeSite -  Professor Rating of Louis Scerbo', tag='a', attrs=[('href', 'http://www.somesite.com/prof.php?pID=478'), ('h', 'ID=SERP,5105.1')])Link(base_url=u'http://www.bing.com/search?q=site%3Asomesite.com', url='http://www.somesite.com/prof.php?pID=527', text='SomeSite -  Professor Rating of Jahan \xe2\x80\xa6', tag='a', attrs=[('href', 'http://www.somesite.com/prof.php?pID=527'), ('h', 'ID=SERP,5118.1')])Link(base_url=u'http://www.bing.com/search?q=site%3Asomesite.com', url='http://www.somesite.com/prof.php?pID=645', text='SomeSite -  Professor Rating of David Kutzik', tag='a', attrs=[('href', 'http://www.somesite.com/prof.php?pID=645'), ('h', 'ID=SERP,5131.1')])

我想获取所有网址,例如:

http://www.somesite.com/prof.php?pID=478
http://www.somesite.com/prof.php?pID=527
http://www.somesite.com/prof.php?pID=645

等等,所以url属性在

如何在我的代码中使用 Mechanize 进一步做到这一点?请记住,将来的某些网址可能如下所示:

http://www.anothersite.com/dir/dir/dir/send.php?pID=100

谢谢!

最佳答案

Mechanize 更像是一个类似于 Python 的浏览器包,为了解析 HTML/XML,我推荐 Lxml,您可以将数据提供给 lxml 并查找 url。另一种选择是使用正则表达式来查找url,这种方法会更灵活。

import re 
url_regex = re.compile('http:[^\']+')
urls = re.findall(url_regex, html_text)

编辑:

不用打印 output,只需在 re.findall() 中传递 output 而不是 html_text 即可然后打印url

关于python - 使用 Mechanize 和 Python Django View 解析 JSON 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18116005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com