【问题标题】:I want to unzip the url for scraping我想解压缩 url 以进行抓取
【发布时间】:2019-06-24 03:53:37
【问题描述】:

"https://www.tokopedia.com/sitemap/product/1.xml.gz" 这是我的网址 这个网址包含产品网址的数量,但它是压缩的 我不知道如何解压缩网址以及如何从中获取数据,如何使用 scrapy 或 Beautiful 解压缩汤其他一些scrapy库

【问题讨论】:

    标签: python beautifulsoup xml-parsing unzip nsxmlparser


    【解决方案1】:

    看看gzip

    import requests
    from io import BytesIO
    import gzip
    r = requests.get('https://www.tokopedia.com/sitemap/product/1.xml.gz',stream=True)
    g=gzip.GzipFile(fileobj=BytesIO(r.content))
    content=g.read()
    print(content)
    

    输出太长,无法在此处粘贴。所以给g.read(1000)输出输出

    输出:

    b'<?xml version="1.0" encoding="UTF-8"?>\n\t<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"\n\txmlns:xhtml="http://www.w3.org/1999/xhtml">\n\t <url>\n\t   <loc>https://www.tokopedia.com/tokoshishaonline/shisha-medium</loc>\n       <xhtml:link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.tokopedia.com/tokoshishaonline/shisha-medium" />\n\t </url>\n\t <url>\n\t   <loc>https://www.tokopedia.com/lighting/lampu-sorot-philips-hnf-207-flood-light-lampu-tembak-lampu-stadion</loc>\n       <xhtml:link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.tokopedia.com/lighting/lampu-sorot-philips-hnf-207-flood-light-lampu-tembak-lampu-stadion" />\n\t </url>\n\t <url>\n\t   <loc>https://www.tokopedia.com/agromedia/pop-supernasa</loc>\n       <xhtml:link rel="alternate" media="only screen and (max-width: 640px)" href="https://m.tokopedia.com/agromedia/pop-supernasa" />\n\t </url>\n\t <url>\n\t   <loc>https://www.tokopedia.com/agromedia/aero-810</loc>\n       <xhtml:l'
    

    【讨论】:

    猜你喜欢
    • 2015-08-05
    • 2021-11-09
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2015-05-05
    相关资源
    最近更新 更多