import urllib.request def gethtml(url='http://www.baidu.com'):
http_handler = urllib.request.HTTPHandler(debuglevel=0) http_redirect_handler=urllib.request.HTTPRedirectHandler() opener = urllib.request.build_opener(http_redirect_handler,http_handler) opener.addheaders=[ ('pragma','no-cache'), ('Content-Type','/') ] request=urllib.request.Request(url) response=opener.open(request) return response.read().decode('utf-8')
from bs4 import BeautifulSoup def find_quote_section(html): soup = BeautifulSoup(html) quote = soup.find('div', attrs={'id': 'wrapper'}) return quote
print(find_quote_section(gethtml())) print(find_quote_section(gethtml()).contents[0]) print(find_quote_section(gethtml()).contents)