V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
virtualenv
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
V2EX  ›  Python

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

  •  
  •   ciba1990 · 2015-07-10 00:50:39 +08:00 · 2680 次点击
    这是一个创建于 3219 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    22 条回复
    ciba1990
        1
    ciba1990  
    OP
       2015-07-10 00:51:43 +08:00
    新手求教。。。。。在线等ing
    wkdhf233
        2
    wkdhf233  
       2015-07-10 00:53:28 +08:00
    完全没看明白你在说啥
    imlonghao
        3
    imlonghao  
       2015-07-10 00:55:06 +08:00 via Android
    No code no bb...
    ciba1990
        4
    ciba1990  
    OP
       2015-07-10 00:55:18 +08:00
    @wkdhf233 就是我在a站抓取了一段<div>代码放到自己网页,<>在我网页源代码现实成&lt,而且不能正常显示网页。
    Septembers
        5
    Septembers  
       2015-07-10 00:57:12 +08:00   ❤️ 1
    ciba1990
        6
    ciba1990  
    OP
       2015-07-10 00:57:33 +08:00
    @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; GUI´s - thenewboston Forum&lt;/a&gt;&lt;/h3&gt;
    &lt;p class=&quot;url&quot;&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  
       2015-07-10 00:58:00 +08:00 via Android
    爬虫代码
    wkdhf233
        8
    wkdhf233  
       2015-07-10 01:01:35 +08:00
    @ciba1990 它转义了你给替换回来呗,连正则都不用。。
    话说第一次见到采集连着html标签一起采的,你拿正则把关键内容切出来然后标签自己输出不就啥事没有了
    ciba1990
        9
    ciba1990  
    OP
       2015-07-10 01:07:33 +08:00
    @wkdhf233 正则怎么用,
    html=urllib2.urlopen(url).read()
    soup = BeautifulSoup(html)

    link = soup.find_all('div')

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

    link = soup.find_all('div')

    mydiv=str(link[0])
    imlonghao
        11
    imlonghao  
       2015-07-10 01:10:07 +08:00 via Android
    import HTMLParser

    html_parser = HTMLParser.HTMLParser()
    s = html_parser.unescape(s)
    imlonghao
        12
    imlonghao  
       2015-07-10 01:10:35 +08:00 via Android
    把mydiv带进去s的地方
    ciba1990
        13
    ciba1990  
    OP
       2015-07-10 01:15:32 +08:00
    @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  
       2015-07-10 01:18:30 +08:00 via Android
    模板被转义了吧
    ciba1990
        15
    ciba1990  
    OP
       2015-07-10 01:19:13 +08:00
    @icedx 怎么解决
    lcqtdwj
        16
    lcqtdwj  
       2015-07-10 01:26:08 +08:00   ❤️ 1
    {% autoescape off %}
    {{ keyword}}
    {% endautoescape %}
    查查文档,就是不要自动转义
    ciba1990
        17
    ciba1990  
    OP
       2015-07-10 01:31:37 +08:00
    @lcqtdwj 美元已发,请查收!
    sallowdish
        18
    sallowdish  
       2015-07-10 02:51:25 +08:00
    要顯示code就放到<pre></pre>裏面,要顯示内容就turn off html escape
    imlonghao
        19
    imlonghao  
       2015-07-10 06:52:07 +08:00 via Android
    Django取消模板转义
    loading
        20
    loading  
       2015-07-10 08:01:44 +08:00 via Android
    flask有自动转,是安全考虑。


    楼主但是说说你用了什么库!
    基本代码都不贴,没人需要你的代码的,都想帮你。
    开源的爬虫代码有很多的。
    thinkmore
        21
    thinkmore  
       2015-07-10 09:52:33 +08:00
    将抓取到的内容进行转义就行了,前后台均可
    ciba1990
        22
    ciba1990  
    OP
       2015-07-10 10:25:00 +08:00
    @loading
    有16楼的方法解决了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1974 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 01:04 · PVG 09:04 · LAX 18:04 · JFK 21:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.