抓取的代码放到<body>里<变成&lt; - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ciba1990

抓取的代码放到<body>里<变成&lt;

  •  
  •   ciba1990 Jul 10, 2015 3473 views
    This topic created in 3944 days ago, the information mentioned may be changed or developed.

    用python从A站抓取一个<div>放到自己的网页上,发现<div>变成<div,而且网页显示的<div>源代码。google说是unicode转译,求教怎么在网页中正常显示抓取的<div>。

    ciba1990
        1
    ciba1990  
    OP
       Jul 10, 2015
    新手求教。。。。。在线等ing
    wkdhf233
        2
    wkdhf233  
       Jul 10, 2015
    完全没看明白你在说啥
    imlonghao
        3
    imlonghao  
       Jul 10, 2015 via Android
    No code no bb...
    ciba1990
        4
    ciba1990  
    OP
       Jul 10, 2015
    @wkdhf233 就是我在a站抓取了一段<div>代码放到自己网页,<>在我网页源代码现实成&lt,而且不能正常显示网页。
    Septembers
        5
    Septembers  
       Jul 10, 2015   1
    ciba1990
        6
    ciba1990  
    OP
       Jul 10, 2015
    @imlonghao


    <html>
    <head>
    </head>
    <body>

    &lt;div class=&quot;searchResults&quot; id=&quot;searchResults&quot;&gt;
    &lt;h2&gt;Web results&lt;/h2&gt;
    &lt;ul&gt;
    &lt;li&gt;
    &lt;h3&gt;&lt;a href=&quot;https://www.python.org/&quot; target=&quot;_blank&quot;&gt;Welcome to Python.org&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&gt;https://www.python.org/&lt;span class=&quot;date&quot;&gt; - 7 hours ago&lt;/span&gt;&lt;/p&gt;
    &lt;p&gt;The official home of the Python Programming Language.&lt;/p&gt;
    &lt;/li&gt;&lt;li class=&quot;sameHostResult&quot;&gt;
    &lt;h3&gt;&lt;a href=&quot;https://www.python.org/downloads/&quot; target=&quot;_blank&quot;&gt;Download Python | Python.org&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&gt;https://www.python.org/downloads/&lt;/p&gt;
    &lt;p&gt;... 2015-05-23 Download Release Notes &lt;br&gt;
    Python 3.4.3 2015-02-25 Download ...&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;&lt;li&gt;
    &lt;h3&gt;&lt;a href=&quot;http://www.pyhton.org/&quot; target=&quot;_blank&quot;&gt;Wrong Page ?&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&gt;http://www.pyhton.org/&lt;/p&gt;
    &lt;p&gt;If you were trying to reach Phyton website please copy and past the following &lt;br&gt;
    URL in your browser: http://www.phyton.org. YOU MAY HAVE GOTTEN HERE BY&lt;br&gt;
    ...&lt;/br&gt;&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;&lt;li&gt;
    &lt;h3&gt;&lt;a href=&quot;http://www.salome-platform.org/forum/forum_10/211874468&quot; target=&quot;_blank&quot;&gt;Creating geometry using &lt;b&gt;pyhton&lt;/b&gt; code SALOME Platform&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&gt;http://www.salome-platform.org/forum/forum_10/211874468&lt;/p&gt;
    &lt;p&gt;Hello everyone!,. I&#39;m almost new in salome; I build up a simple geometry (n &lt;br&gt;
    nodes and n-1 beams) using the salome gui. It took me a long time; then I &lt;br&gt;
    discovered ...&lt;/br&gt;&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;&lt;li&gt;
    &lt;h3&gt;&lt;a href=&quot;http://developers.gigya.com/display/GD/Pyhton+SDK+Change+Log&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;Pyhton&lt;/b&gt; SDK Change Log - Gigya Documentation - Developers Guide&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&gt;http://developers.gigya.com/display/GD/Pyhton+SDK+Change+Log&lt;/p&gt;
    &lt;p&gt;Jun 10, 2015 &lt;b&gt;...&lt;/b&gt; Version 2.17 - 26 Apr 2015. Bug fix regarding URL encoding. The Python SDK &lt;br&gt;
    now restores urllib handlers after completing requests to Gigya.&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;&lt;li&gt;
    &lt;h3&gt;&lt;a href=&quot;
    &quot; target=&quot;_blank&quot;&gt;&lt;b&gt;Pyhton&lt;/b&gt; - You A Me LifeIine Full Promo Dancehall 2015 - YouTube&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&gt;
    &lt;/p&gt;
    &lt;p&gt;Feb 16, 2015 &lt;b&gt;...&lt;/b&gt; &lt;b&gt;Pyhton&lt;/b&gt; - You A Me LifeIine ○Full Promo○ Dancehall 2015. IamDjChigga ... Up &lt;br&gt;
    Hot DJ Chigga &lt;b&gt;Pyhton&lt;/b&gt; A Good Artists the Thing Loud...$$$$$.&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;&lt;li class=&quot;sameHostResult&quot;&gt;
    &lt;h3&gt;&lt;a href=&quot;
    &quot; target=&quot;_blank&quot;&gt;&lt;b&gt;Pyhton&lt;/b&gt; - Mommy Nah Worry No More Full Promo Dancehall 2015 &lt;b&gt;...&lt;/b&gt;&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&gt;
    &lt;/p&gt;
    &lt;p&gt;Mar 20, 2015 &lt;b&gt;...&lt;/b&gt; &lt;b&gt;Pyhton&lt;/b&gt; - Mommy Nah Worry No More ○Full Promo○ Dancehall 2015. &lt;br&gt;
    IamDjChigga. SubscribeSubscribedUnsubscribe ...&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;&lt;li&gt;
    &lt;h3&gt;&lt;a href=&quot;https://www.thenewboston.com/forum/topic.php?id=6569&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;Pyhton&lt;/b&gt; GUIs - thenewboston Forum&lt;/a&gt;&lt;/h3&gt;
    https://www.thenewboston.com/forum/topic.php?id=6569&lt;/p&gt;
    &lt;p&gt;May 2, 2015 &lt;b&gt;...&lt;/b&gt; Can anyone recommend a good book( i.e. as in paper) to use as a reference &lt;br&gt;
    work with Python GUis. There are lots of excellent videos etc on ...&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;&lt;li&gt;
    &lt;h3&gt;&lt;a href=&quot;http://www.gamefaqs.com/psp/932978-metal-gear-solid-portable-ops/answers/189967-how-do-i-beat-pyhton&quot; target=&quot;_blank&quot;&gt;How do I beat &lt;b&gt;pyhton&lt;/b&gt;? - Metal Gear Solid: Portable Ops Answers for &lt;b&gt;...&lt;/b&gt;&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot; title=&quot;http://www.gamefaqs.com/psp/932978-metal-gear-solid-portable-ops/answers/189967-how-do-i-beat-pyhton&quot;&gt;http://www.gamefaqs.com/psp/932978-metal-gear-solid-portable-ops/answe...&lt;/p&gt;
    &lt;p&gt;For Metal Gear Solid: Portable Ops on the PSP, a GameFAQs Answers question &lt;br&gt;
    titled &quot;How do I beat &lt;b&gt;pyhton&lt;/b&gt;?&quot;.&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;&lt;li&gt;
    &lt;h3&gt;&lt;a href=&quot;https://bugs.launchpad.net/bugs/1415067&quot; target=&quot;_blank&quot;&gt;Bug #1415067 “QtiPlot crashed when chossing &lt;b&gt;Pyhton&lt;/b&gt; as default sc &lt;b&gt;...&lt;/b&gt;&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&gt;https://bugs.launchpad.net/bugs/1415067&lt;/p&gt;
    &lt;p&gt;Jan 27, 2015 &lt;b&gt;...&lt;/b&gt; I installed qtiplot and worked on it for a while. Changing the Default scripting &lt;br&gt;
    language to &lt;b&gt;Pyhton&lt;/b&gt; in Preferences, I end with this problem.&lt;/br&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;/ul&gt;
    &lt;/div&gt;

    </body>
    </html>
    imlonghao
        7
    imlonghao  
       Jul 10, 2015 via Android
    爬虫代码
    wkdhf233
        8
    wkdhf233  
       Jul 10, 2015
    @ciba1990 它转义了你给替换回来呗,连正则都不用。。
    话说第一次见到采集连着html标签一起采的,你拿正则把关键内容切出来然后标签自己输出不就啥事没有了
    ciba1990
        9
    ciba1990  
    OP
       Jul 10, 2015
    @wkdhf233 正则怎么用,
    html=urllib2.urlopen(url).read()
    soup = BeautifulSoup(html)

    link = soup.find_all('div')

    mydiv=str(link[0])
    这是我爬虫代码,新手上路。
    ciba1990
        10
    ciba1990  
    OP
       Jul 10, 2015
    @imlonghao
    html=urllib2.urlopen(url).read()
    soup = BeautifulSoup(html)

    link = soup.find_all('div')

    mydiv=str(link[0])
    imlonghao
        11
    imlonghao  
       Jul 10, 2015 via Android
    import HTMLParser

    html_parser = HTMLParser.HTMLParser()
    s = html_parser.unescape(s)
    imlonghao
        12
    imlonghao  
       Jul 10, 2015 via Android
    把mydiv带进去s的地方
    ciba1990
        13
    ciba1990  
    OP
       Jul 10, 2015
    @imlonghao
    用的django
    views.py
    def index(request):
    template = loader.get_template('python.html')
    cOntext= Context({'keyword':mydiv})
    return HttpResponse(template.render(context))

    python.html
    <html>
    <head>
    </head>
    <body>{{keyword}}</body>
    </html>
    icedx
        14
    icedx  
       Jul 10, 2015 via Android
    模板被转义了吧
    ciba1990
        15
    ciba1990  
    OP
       Jul 10, 2015
    @icedx 怎么解决
    lcqtdwj
        16
    lcqtdwj  
       Jul 10, 2015   1
    {% autoescape off %}
    {{ keyword}}
    {% endautoescape %}
    查查文档,就是不要自动转义
    ciba1990
        17
    ciba1990  
    OP
       Jul 10, 2015
    @lcqtdwj 美元已发,请查收!
    sallowdish
        18
    sallowdish  
       Jul 10, 2015
    要示code就放到<pre></pre>面,要示内容就turn off html escape
    imlonghao
        19
    imlonghao  
       Jul 10, 2015 via Android
    Django取消模板转义
    loading
        20
    loading  
       Jul 10, 2015 via Android
    flask有自动转,是安全考虑。


    楼主但是说说你用了什么库!
    基本代码都不贴,没人需要你的代码的,都想帮你。
    开源的爬虫代码有很多的。
    thinkmore
        21
    thinkmore  
       Jul 10, 2015
    将抓取到的内容进行转义就行了,前后台均可
    ciba1990
        22
    ciba1990  
    OP
       Jul 10, 2015
    @loading
    有16楼的方法解决了
    About     Help     Advertise     Blog     API     FAQ     Solana     5177 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 46ms UTC 09:42 PVG 17:42 LAX 02:42 JFK 05:42
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86