BeautifulSoup 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">
阅读 4.3k
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')
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题