python爬取网页源代码怎么看_网页爬虫 - 【如图】python爬取的html页面和浏览器显示源码的结果不同...

问 题

如图所示,用python爬取的html页面和浏览器显示的内容有些出入。

照理说爬取的同样是服务器端静态的html页面,怎么会有不同呢?

网站地址:点我点我

爬虫源码:

# -*-coding:UTF-8-*-

from urllib.request import urlopen

from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")

bsObj = BeautifulSoup(html,"lxml")

for child in bsObj.find("",{"id": "giftList"}).contents:

print(child)

bVCtYI

这个问题可能有点“孔乙己”,不过我确实挺好奇它的原因的,希望有人能不吝赐教

解决方案

经过实测,结论是 bs4 改变了属性的顺序。

1、在浏览器中右键点击页面,选:

审查元素

bVCvsb

查看网页源码

bVCvsc

2、在 python3 程序中对比:

import re

ptn_tr = re.compile(r'

import requests as req

rsp=req.get('http://www.pythonscraping.com/pages/page3.html')

html = rsp.text

print('requests:\t', ptn_tr.findall(html)[0])

from urllib.request import urlopen

rsp = urlopen("http://www.pythonscraping.com/pages/page3.html")

html = rsp.read().decode()

print('urlopen:\t', ptn_tr.findall(html)[0])

from bs4 import BeautifulSoup

html = str(BeautifulSoup(html,"lxml"))

print('bs4Soup:\t', ptn_tr.findall(html)[0])

结果:

requests:

urlopen:

bs4Soup:

wx.jpg

扫一扫关注IT屋

微信公众号搜索 “ IT屋 ” ,选择关注与百万开发者在一起

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值