首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python从检索页的标题中删除不需要的文本

如何使用python从检索页的标题中删除不需要的文本
EN

Stack Overflow用户
提问于 2022-01-11 15:31:39
回答 1查看 271关注 0票数 0

嗨,我已经编写了一个python程序来检索页面的标题--它工作得很好--但是对于一些页面,它也会收到一些不想要的文本--如何避免这种情况。

这是我的节目

代码语言:javascript
复制
# importing the modules
import requests
from bs4 import BeautifulSoup

# target url
url = 'https://atlasobscura.com'

# making requests instance
reqs = requests.get(url)

# using the BeaitifulSoup module
soup = BeautifulSoup(reqs.text, 'html.parser')

# displaying the title
print("Title of the website is : ")
for title in soup.find_all('title'):
    title_data = title.get_text().lower().strip()
    print(title_data)

这是我的输出

代码语言:javascript
复制
atlas obscura - curious and wondrous travel destinations
aoc-full-screen
aoc-heart-solid
aoc-compass
aoc-flipboard
aoc-globe
aoc-pocket
aoc-share
aoc-cancel
aoc-video
aoc-building
aoc-clock
aoc-clipboard
aoc-help
aoc-arrow-right
aoc-arrow-left
aoc-ticket
aoc-place-entry
aoc-facebook
aoc-instagram
aoc-reddit
aoc-rss
aoc-twitter
aoc-accommodation
aoc-activity-level
aoc-add-a-photo
aoc-add-box
aoc-add-shape
aoc-arrow-forward
aoc-been-here
aoc-chat-bubbles
aoc-close
aoc-expand-more
aoc-expand-less
aoc-forum-flag
aoc-group-size
aoc-heart-outline
aoc-heart-solid
aoc-home
aoc-important
aoc-knife-fork
aoc-library-books
aoc-link
aoc-list-circle-bullets
aoc-list
aoc-location-add
aoc-location
aoc-mail
aoc-map
aoc-menu
aoc-more-horizontal
aoc-my-location
aoc-near-me
aoc-notifications-alert
aoc-notifications-mentions
aoc-notifications-muted
aoc-notifications-tracking
aoc-open-in-new
aoc-pencil
aoc-person
aoc-pinned
aoc-plane-takeoff
aoc-plane
aoc-print
aoc-reply
aoc-search
aoc-shuffle
aoc-star
aoc-subject
aoc-trip-style
aoc-unpinned
aoc-send
aoc-phone
aoc-apps
aoc-lock
aoc-verified

而不是这个,我应该只收到这一行

代码语言:javascript
复制
"atlas obscura - curious and wondrous travel destinations"

请帮助我一些想法,所有其他网站都在工作,只有一些网站有这些问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-11 15:43:03

您的问题是,您正在找到所有的“标题”在页面中出现。“美丽的汤”有一个属性title,专门用于您想要做的事情。以下是您修改的代码:

代码语言:javascript
复制
# importing the modules
import requests
from bs4 import BeautifulSoup

# target url
url = 'https://atlasobscura.com'

# making requests instance
reqs = requests.get(url)

# using the BeaitifulSoup module
soup = BeautifulSoup(reqs.text, 'html.parser')
title_data = soup.title.text.lower()

# displaying the title
print("Title of the website is : ")
print(title_data)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70669378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档