建立一个应用程序,给我一些我可以点击的网页标题

2024-09-18 22:36:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想建立一个简单的应用程序,看起来像这样

a web app

我会给它一个URl(最后的数字是该页面的编号),它会给我100个页面标题,我可以点击(从特定网站获取的标题)并在新选项卡中打开。每一页都有如下内容:

<meta property="article:title" content="title that i wanna see"> <meta property="og:title" content="title that i wanna see">

我懂一点javascript、python和HTML,这将是我的第一个项目!请告诉我怎么开始


Tags: 应用程序url标题thattitle网站数字property
2条回答
import requests
from bs4 import BeautifulSoup

urls=[]
titles=[]
html="""<html>
    <body>"""

def chtml(url,title):
    global html
    a="""
    <p dir="rtl" align="center">
        <a target='blank' href='"""+url+"""'>
            """+title+"""
        </a>
    </p>
    <br/>\n"""
    html+=a

def end():
    global html
    html+="""
    </body>
</html>"""
    f=open('index.html','w',encoding="utf-8")
    f.write(html)
    f.close()
    print('program is ended. go to index.html')


def get_title(number):
    url='http://example.com/fa/features/'+str(number)
    try:
        page=requests.get(url)
        soup = BeautifulSoup(page.content,"html.parser")
        name = soup.find("head")
        name=str(name)
        a=name.split('\n')[4]
        text=''
        for i in range(0,len(a)):
            if a[i]+a[i+1]=='t=':
                for i2 in range(i+3,len(a)):
                    if a[i2]!='"':
                        text+=a[i2]
                    else:
                        break
                break
        urls.append(url)
        titles.append(text)
        print('got : '+url)
    except:
        pass

if __name__=='__main__':
    a=int(input('enter numer from : '))
    b=int(input('enter numer to : '))
    for i in range(a,b+1):
        get_title(i)
    for i3 in range(0,len(urls)):
        if titles[i3]!='':
            chtml(urls[i3],titles[i3])
    end()

你的基本HTML5应该能够做到这一点。然后在css中使用boostrap。Google html“a ref tag”获取标签的详细信息

相关问题 更多 >