css - BeautifulSoup python 爬虫无法搜索目标标签
怪我咯
怪我咯 2017-04-17 17:48:15
[Python讨论组]

代码

import requests
from bs4 import BeautifulSoup

url = 'http://product.pconline.com.cn/mobile/'
response = requests.get(url)
html = response.text
print html

soup = BeautifulSoup(html, 'lxml')
site = soup.find_all('img', class_="pic")
print site

目标网站:http://product.pconline.com.cn/mobile/
打算爬取的是手机图片标签,运行上面这段代码后打印的site得到是空的。

图片部分html片段:

<img class="pic" alt="华为Mate8/3GB+32GB版" title="麒麟950处理器、6寸超大屏、超高屏占比、超窄边框,3GB+32GB全网通版本" src="http://img.pconline.com.cn/images/product/5807/580761/q_sn8.jpg" height="150" width="200">
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(3)
怪我咯

我这边是可以的,不知道会不会是编码的问,或者是lxml扩展的问题

伊谢尔伦

换个解析器试试
soup = BeautifulSoup(html, 'html.parser')

怪我咯

pyquery,不二之选, 语法和jquery一样

import requests, pyquery
url = 'http://product.pconline.com.cn/mobile/'
r = requests.get(url)

html = r.text.replace('#src', 'jsrc')
Q = pyquery.PyQuery(html)

for _ in Q('img.pic'):
    print Q(_).attr('jsrc'), Q(_).attr('alt'), Q(_).attr('title')
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号