【发布时间】:2019-07-13 20:18:09
【问题描述】:
在我的text_scraper(page_soup) 中,我意识到最后我得到了与我的文章完全无关的不相关信息。什么是消除不相关信息的通用方法?
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import re
# Initializing our dictionary
dictionary = {}
# Initializing our url key
url_key = 'url'
dictionary.setdefault(url_key, [])
# Initializing our text key
text_key = 'text'
dictionary.setdefault(text_key, [])
def text_scraper(page_soup):
text_body = ''
# Returns the text of p tags, we stopped it at -5 bc that's when the text is irrelevant to the article
for p in page_soup.find_all('p'):
text_body += p.text
return(text_body)
def article_scraper(url):
# Opening up the connection, grabbing the page
uClient = uReq(url)
page_html = uClient.read()
uClient.close()
# HTML parsing
page_soup = soup(page_html, "html.parser")
dictionary['url'].append(url)
dictionary['text'] = text_scraper(page_soup)
return dictionary
articles_zero = 'https://www.sfchronicle.com/news/bayarea/heatherknight/article/Special-education-teacher-a-prime-example-of-13560483.php'
article = article_scraper(articles_zero)
article
【问题讨论】:
-
你想抓取文章的哪一部分?
-
直到这一段:“这座城市需要尽其所能来留住像 Torres Esquer 这样的教师。恰好有 1.85 亿美元的免费资金坐在那里帮助它做到这一点。”
-
@Andy 我的回答对你有好处吗?如果是,你能接受吗?
标签: python-3.x beautifulsoup html-parsing