问题
I'm trying to get text from articles on various webpages and write them as clean text documents. I don't want all visible text because that often includes irrelevant links on the side of webpages. I'm using Beautifulsoup to extract the information from pages. But, extra links not just on the side of the page but also those sometimes in the middle of the body text and at the bottom of the articles sometimes make it into the final product.
Does anyone know how to deal with the problem of extra links that are converted into text that are not actually a part of the real article's text?
#Some of the imports are for other portions of the code not shown here.
#I'm new to Python and am bad at remembering which library has which functions.
import os
import sys
import urllib2
import webbrowser
from bs4 import BeautifulSoup
from os import path
from cookielib import CookieJar
#I made an opener to deal with proxies and put *** instead of my information
#cookielib helps me get articles from nytimes
proxy = urllib2.ProxyHandler({'http': '***' % '***'})
auth = urllib2.HTTPBasicAuthHandler()
cj = CookieJar()
opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler, urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
#Uses url input as a string to upen a webpage and and pulls out all the information.
def baumeister(url):
req = urllib2.Request(url)
opened = urllib2.urlopen(req)
html_doc = opened.read()
soup = BeautifulSoup(html_doc)
return soup
#Gets the body text from that html information.
def substanz(url):
soup = baumeister(url)
body = soup.find_all("p") #This is where I have tried to fix the problem and failed
result = ""
for e in body:
i = e.getText().replace("\t", "").replace(" ", " ").strip().encode(errors="ignore")
result += i + "\r\n\r\n"
return result
One article that I have used to test substanz that gets cleaned in the exact way I want is:
http://blogs.hbr.org/2014/06/do-you-really-want-to-be-yourself-at-work/
I'm trying to test with more articles from different sites. So I'm trying to clean the result of substanz (the result is a big string). The problem I have is with this article:
http://www.cnbc.com/id/101790001?__source=yahoo%7Cfinance%7Cheadline%7Cheadline%7Cstory&par=yahoo&doc=101790001%7CThink%20college%20is%20expensiv#.
I've just used the print substanz('url')
to see what the result looks like. With the cnbc article I get extra links turned into text that are not really a part of the article. Whereas in the Harvard Business Review Article everything works out just fine as included links are part of the actual text.
I'm not going to attach the full result for each article here for viewing because they are each a full page of text long.
If you try exactly the code I have posted above the opener is not going to work, so use whatever opener you like to access websites. I have to access a certain proxy at work so that's the format that works for me.
Final note, I'm using python 3.4, and am writing the code in ipython notebook.
回答1:
import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.cnbc.com/id/101790001?__source=yahoo%7Cfinance%7Cheadline%7Cheadline%7Cstory&par=yahoo&doc=101790001%7CThink%20college%20is%20expensiv#")
soup = BeautifulSoup(r.content)
text =[''.join(s.findAll(text=True))for s in soup.findAll('p')]
print (text)
['>> View All Results for ""', 'Enter multiple symbols separated by commas', 'London quotes now available', 'Interest rates on loans to jump', "Because federal student loans are tied to the 10-year Treasury note, CNBC's Sharon Epperson reports borrowers will see the impact of the rise in Treasury yields over the past year.", ' Congratulations, graduates, on your diploma. Now what about that $29,000 student loan debt? ', ' More than 70 percent of graduates will carry student debt into the real world, according to the Institute for College Access and Success. And the average debt is just shy of $30,000. ', ' But the news will get worse next week when interest rates on student loans are set to rise again. ', ' Though federal student loan rates are fixed for the life of the loan, these rates reset for new borrowers every July 1, thanks to legislation that ties the rates to the performance of the financial markets. ', ' The interest rate on federal Stafford loans will go from its current fixed rate of just under 4 percent to 4.66 percent for loans that are distributed between July 1 and June 30, 2015. ', ' Read MoreStudent loan problem an easy fix: Sen. Warren ', ' For graduate students, the rate on Stafford loans will rise from just over 5 percent to 6.21 percent. ', ' Direct PLUS Loans for graduates and parents are still the most expensive, with rates rising to 7.21 percent.', 'Which college major pays off most?', "CNBC's Sharon Epperson reports majoring in engineering is the most lucrative. ", " The increase in monthly federal student loan payments can add up quickly, but shouldn't be too burdensome for most students. For every $10,000 in loans, new borrowers will pay about $4 more a month based on a 10-year repayment period. ", " Read MoreWhy millennial women don't save for retirement ", ' Still, experts warn that this is only just the beginning. ', ' "Federal student loan rates will continue to increase in the next few years and will likely hit the maximum rate caps which are as high as 10.5 percent for some loans," said Mark Kantrowitz, senior vice president and publisher of Edvisors.com. ', ' For sophomore student Samantha Cook, the decision to go to George Washington University was a big one financially. She says she had doubts about it. ', ' "My parents wanted to assure me that no matter what I picked, we\'d find a way to make it work," Cook said. Like most families, Cook and her parents are making it work by combining their household savings, scholarships and grants—and student loans. ', ' Read MoreCramer: Offset high cost of higher education ', ' Despite rising tuition and borrowing costs, the Cook family decided against Samantha transferring to an in-state university. ', ' Despite the debt load she is taking on, she said, "the value of a GW degree for me at least would be more valuable when looking for jobs later on." ', " —By CNBC's Sharon Epperson ", 'Hosting a yard sale may not be the most profitable way to get rid of your old junk.', 'Many Americans with debit cards tied to their checking accounts are still confused about how these programs work. ', "Here's how to avoid these deadly sins if you're contemplating or already in a divorce.", "The IRS offers a lot of help for students. Problem is, the educational tax breaks and how they work together -- or don't -- are confusing.", 'Get the best of CNBC in your inbox', 'Tips for home buyers that will help you find the right home for your bank account.', 'Complaints about movers are down. How to find the right one—and save.', "Forget bathing suit season. Why it's really time to join the gym. ", 'Drivers might see lower gas prices this year, but smart shopping tactics could help them save even more.', 'Data is a real-time snapshot *Data is delayed at least 15 minutesGlobal Business and Financial News, Stock Quotes, and Market Data and Analysis', '© 2014 CNBC LLC. All Rights Reserved.', 'A Division of NBCUniversal']
From the website in your link to get text from the main article.
import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.cnbc.com/id/101790001?__source=yahoo%7Cfinance%7Cheadline%7Cheadline%7Cstory&par=yahoo&doc=101790001%7CThink%20college%20is%20expensiv#")
soup = BeautifulSoup(r.content)
text =[''.join(s.findAll(text=True)) for s in soup.findAll("div", {"class":"group"})]
print (text)
['\n Congratulations, graduates, on your diploma. Now what about that $29,000 student loan debt? \n More than 70 percent of graduates will carry student debt into the real world, according to the Institute for College Access and Success. And the average debt is just shy of $30,000. \n But the news will get worse next week when interest rates on student loans are set to rise again. \n Though federal student loan rates are fixed for the life of the loan, these rates reset for new borrowers every July 1, thanks to legislation that ties the rates to the performance of the financial markets. \n The interest rate on federal Stafford loans will go from its current fixed rate of just under 4 percent to 4.66 percent for loans that are distributed between July 1 and June 30, 2015. \n Read MoreStudent loan problem an easy fix: Sen. Warren \n For graduate students, the rate on Stafford loans will rise from just over 5 percent to 6.21 percent. \n Direct PLUS Loans for graduates and parents are still the most expensive, with rates rising to 7.21 percent.\n', '\n The increase in monthly federal student loan payments can add up quickly, but shouldn\'t be too burdensome for most students. For every $10,000 in loans, new borrowers will pay about $4 more a month based on a 10-year repayment period. \n Read MoreWhy millennial women don\'t save for retirement \n Still, experts warn that this is only just the beginning. \n "Federal student loan rates will continue to increase in the next few years and will likely hit the maximum rate caps which are as high as 10.5 percent for some loans," said Mark Kantrowitz, senior vice president and publisher of Edvisors.com. \n For sophomore student Samantha Cook, the decision to go to George Washington University was a big one financially. She says she had doubts about it. \n "My parents wanted to assure me that no matter what I picked, we\'d find a way to make it work," Cook said. Like most families, Cook and her parents are making it work by combining their household savings, scholarships and grants—and student loans. \n Read MoreCramer: Offset high cost of higher education \n Despite rising tuition and borrowing costs, the Cook family decided against Samantha transferring to an in-state university. \n Despite the debt load she is taking on, she said, "the value of a GW degree for me at least would be more valuable when looking for jobs later on." \n —By CNBC\'s Sharon Epperson \n']
来源:https://stackoverflow.com/questions/24458353/cleaning-text-string-after-getting-body-text-using-beautifulsoup