簡體   English   中英

從文件中的URL讀取內容

[英]Reading in Content From URLS in a File

我正在嘗試從主URL獲取其他子集URL。 但是,當我打印以查看是否得到內容時,我注意到我只是得到HTML,而不是其中的URL。

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

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

我認為這就是您想要的。 您可以使用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')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM