【问题标题】:Reading in Content From URLS in a File从文件中的 URL 读取内容
【发布时间】:2018-07-26 05:08:20
【问题描述】:

我正在尝试从主 URL 获取其他子集 URL。但是,当我打印以查看是否获得内容时,我注意到我只获得了 HTML,而不是其中的 URL。

import urllib
file = 'http://example.com'

with urllib.request.urlopen(file) as url:
    collection = url.read().decode('UTF-8')

【问题讨论】:

  • 内容是否被javascript加载?
  • “url_file”应该是“url”?
  • 是的,它正在被javascript加载
  • 您是否要获取渲染页面的图片? HTML 是内容,如果您想要更多内容,可以使用 Selenium 或 Chrome Headless
  • 抱歉措辞不好。我想要完成的是获取一个 URL 并在主 URL 中提取 URL。

标签: python python-3.x python-requests urllib


【解决方案1】:

我认为这就是您要寻找的。 你可以使用漂亮的 python 汤库,这段代码应该适用于 python3

    import urllib
    from urllib.request import urlopen
    from bs4 import BeautifulSoup

    def get_all_urls(url):
        open = urlopen(url)
        url_html = BeautifulSoup(open, 'html.parser')
        for link in url_html.find_all('a'):
            links = str(link.get('href'))
            if links.startswith('http'):
                print(links)
            else:
                print(url + str(links))
    get_all_urls('url.com')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-27
    • 2017-04-19
    • 2012-02-25
    • 2011-07-28
    • 2013-03-18
    • 2017-08-13
    • 2015-08-26
    相关资源
    最近更新 更多