request - Data fetch from url using urllib in python -
i trying fetch data url : "http://www.sears.com/search=refrigerators"
this tried:
>>> cookielib import cookiejar >>> import urllib >>> import urllib2 >>> bs4 import beautifulsoup >>> data = {} >>> data['search'] = 'refrigerators' >>> url_values = urllib.urlencode(data) >>> cj = cookiejar() >>> opener = urllib2.build_opener(urllib2.httpcookieprocessor(cj)) >>> url = 'http://www.sears.com' >>> full_url = url + '/' + url_values >>> f = opener.open(full_url).read() >>> soup = beautifulsoup(f, "html.parser") >>> print(soup.title) <title>shopping tourism: shop internationally @ sears</title> >>> f = opener.open(full_url).read() >>> soup = beautifulsoup(f, "html.parser") >>> print(soup.title) <title>refrigerators sears.com</title>
i getting different title instead of getting same :(. (may getting title home page first)
why happening? please me search page data.
i suggest using requests session object, version of cookiejar, gets title of refrigerators sears.com
:
import requests bs4 import beautifulsoup s = requests.session() r = s.get("http://www.sears.com/search=refrigerators") soup = beautifulsoup(r.content) print soup.title
Comments
Post a Comment