如何从HTML获取href链接并使用Python下载文件?

时间:2018-05-31 11:24:38

标签: python parsing beautifulsoup

如何解析html中的所有链接,其中包含* .rar文件的链接并下载所有这些文件?

我需要解析html中的3个链接:

<a class="zlink" href="http://example.com/lv/20180530/L10.rar">
<a class="zlink" href="http://example.com/est/20180530/ES10.rar">
<a class="zlink" href="http://example.com/pol/20180530/PL10.rar">

Html包含另一种文件类型,例如favicon或hmtl。

import requests
import urllib
import random
import threading
from BeautifulSoup import BeautifulSoup

user_agent_list = [
    #Windows
    'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Firefox/60.0',
    #Linux
    'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:60.0) Gecko/20100101 Firefox/60.0',
]
url = 'http://example.com'

#Using Requests 
# establishing session
s = requests.session() 
user_agent = random.choice(user_agent_list)
headers = {
    'login_username': 'user',
    'login_password': 'pass',
    'User-Agent': user_agent}
response = requests.get(url,headers=headers)
print(response.content)

soup = BeautifulSoup(response.content)
for link in soup.findAll('a', attrs={'href': re.compile("$rar")})
    r = requests.get('href')    

0 个答案:

没有答案
相关问题