beautifulsoup - python爬虫获取不到标签内容

查看:2462
本文介绍了beautifulsoup - python爬虫获取不到标签内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

链接如下:
http://aaxxy.com/vod-detail-i...
使用requests请求此连接,然后用BeautifulSoup解析获取 dl > dd > a 标签的内容,其中:

上图所示的4个 a 标签的内容只能获取到第一个电影,后面三个动作喜剧剧情获取不到,输出结果为None:

使用pyquery解析的话连None都不显示,直接跳过这三个标签了:

请问为什么会这样?

代码如下:
bs4解析:

web_data = requests.get('http://aaxxy.com/vod-detail-id-10.html ')
web_data.encoding = 'utf-8'
soup = BeautifulSoup(web_data.text,'lxml')
actor = soup.select('dl > dd > a')
print(actor)
for i in actor:
    print(i.string)

pyquery解析:

web_data = requests.get('http://aaxxy.com/vod-detail-id-10.html ')
web_data.encoding = 'utf-8'
pq = pyquery.PyQuery(web_data.text)
print(pq('dl > dd >a').text())

解决方案

因为取到的html A标签不规整,只有电影这个标签是规整的,其他的A标记都提前闭合了

<a href="/vod-type-id-1-pg-1.html">电影</a>
<a target='_blank' href='/vod-list-id-1-pg-1-order--by--class-59-year--letter--area--lang-.html' />动作</a>
<a target='_blank' href='/vod-list-id-1-pg-1-order--by--class-63-year--letter--area--lang-.html' />喜剧</a>
<a target='_blank' href='/vod-list-id-1-pg-1-order--by--class-83-year--letter--area--lang-.html' />剧情</a>

这篇关于beautifulsoup - python爬虫获取不到标签内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆