我正在尝试从返回的许多 URL 中提取特定文本。 我将 Python 2.7 与请求和 BeautifulSoup 结合使用。
原因是我需要找到最新的 URL,它可以由以下 url 中的最高数字“DF_7”识别,其中 7 是最高的。然后将下载此 url。 请注意,每天都会添加新文件,这就是我需要检查编号最高的文件的原因。
一旦我在 URL 列表中找到最高编号,我就需要加入 这个“https://service.rl360.com/scripts/customer.cgi/SC/servicing/”到具有最高数字的 url。 最终产品应该是这样的。 https://service.rl360.com/scripts/customer.cgi/SC/servicing/downloads.php?Reference=DF_7&SortField=ExpiryDays&SortOrder=Ascending
url 看起来像这样,只是 DF_ 每次递增
这是正确的方法吗?如果是这样,我该怎么做。
谢谢
import base
import requests
import zipfile, StringIO, re
from lxml import html
from bs4 import BeautifulSoup
from base import os
from django.conf import settings
# Fill in your details here to be posted to the login form.
payload = {
'USERNAME': 'xxxxxx',
'PASSWORD': 'xxxxxx',
'option': 'login'
}
headers = {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
# Use 'with' to ensure the session context is closed after use.
with requests.Session() as s:
p = s.post('https://service.rl360.com/scripts/customer.cgi?option=login', data=payload)
# An authorised request.
r = s.get('https://service.rl360.com/scripts/customer.cgi/SC/servicing/downloads.php?Folder=DataDownloads&SortField=ExpiryDays&SortOrder=Ascending', stream=True)
content = r.text
soup = BeautifulSoup(content, 'lxml')
table = soup.find('table')
links = table.find_all('a')
print links
请您参考如下方法:
您可以直接转到类“tableid”的最后一个链接并打印它的 href 值,如下所示:
href = soup.find_all("a", {'class':'tabletd'})[-1]['href']
base = "https://service.rl360.com/scripts/customer.cgi/SC/servicing/"
print (base + href)
