推荐学习书目
› 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 · Jul 10, 2015 · 4146 views
    This topic created in 4144 days ago, the information mentioned may be changed or developed.

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

    ciba1990
    ciba1990
    OP
    Jul 10, 2015
    1
    新手求教。。。。。在线等ing
    wkdhf233
    wkdhf233
    Jul 10, 2015
    2
    完全没看明白你在说啥
    imlonghao
    imlonghao
    Jul 10, 2015 via Android
    3
    No code no bb...
    ciba1990
    ciba1990
    OP
    Jul 10, 2015
    4
    @wkdhf233 就是我在a站抓取了一段<div>代码放到自己网页,<>在我网页源代码现实成&lt,而且不能正常显示网页。
    ciba1990
    ciba1990
    OP
    Jul 10, 2015
    6
    @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
    imlonghao
    Jul 10, 2015 via Android
    7
    爬虫代码
    wkdhf233
    wkdhf233
    Jul 10, 2015
    8
    @ciba1990 它转义了你给替换回来呗,连正则都不用。。
    话说第一次见到采集连着html标签一起采的,你拿正则把关键内容切出来然后标签自己输出不就啥事没有了
    ciba1990
    ciba1990
    OP
    Jul 10, 2015
    9
    @wkdhf233 正则怎么用,
    html=urllib2.urlopen(url).read()
    soup = BeautifulSoup(html)

    link = soup.find_all('div')

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

    link = soup.find_all('div')

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

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


    楼主但是说说你用了什么库!
    基本代码都不贴,没人需要你的代码的,都想帮你。
    开源的爬虫代码有很多的。
    thinkmore
    thinkmore
    Jul 10, 2015
    21
    将抓取到的内容进行转义就行了,前后台均可
    ciba1990
    ciba1990
    OP
    Jul 10, 2015
    22
    @loading
    有16楼的方法解决了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2119 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 13:08 · PVG 21:08 · LAX 05:08 · JFK 08:08
    ♥ Do have faith in what you're doing.