开发者社区> 问答> 正文

使用beautifulsoup框架进行Python html解析

我正在使用Beauitful汤框架检索链接(以下html内容的参考)

     ```js
我使用以下代码在python中检索了此代码:

 ```js
pageFile = urllib.urlopen("appannie.com/apps/google-play/app/com.opera.mini.android")
 pageHtml = pageFile.read()
 pageFile.close()
 print pageHtml
 soup = BeautifulSoup("".join(pageHtml))
 item = soup.find("a", {"title":"Open in Google Play"})

 print item

我得到NoneType作为输出。任何帮助都将非常棒。

我打印出html页面,输出如下:

<html>
<head><title>503 Service Temporarily Unavailable</title></head>
<body bgcolor="white">
<center><h1>503 Service Temporarily Unavailable</h1></center>
<hr><center>nginx</center>
</body>
</html>

在浏览器上工作正常

展开
收起
祖安文状元 2020-02-22 15:42:15 765 0
1 条回答
写回答
取消 提交回答
  • item = soup.find("a", {"title":"Open in Google Play"})
    
    

    您最初搜索的是标题为“在Google Play中打开”的“跨度”,但是要查找的元素是“ a”(链接)。

    编辑:由于服务器似乎显示了503错误,请尝试使用此代码设置一个公共用户代理(未经测试,它可能根本无法工作;您需要import urllib2):

    soup = BeautifulSoup(urllib2.urlopen(urllib2.Request(sampleURL, None, {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0"})).read())
    item = soup.find("a", {"title":"Open in Google Play"}) 
    print item
    
    

    我也删除了无用的,"".join(pageHtml)因为urllib2已经返回了字符串,所以不需要加入。

    2020-02-22 15:42:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载

相关镜像