1 #!usr/bin/env python2 #Coding:utf-83 4 ImportUrllib25 ImportCookielib6 fromBs4ImportBeautifulSoup7 8 #cookie = cookielib. Cookiejar ()9 #hander = Urllib2. Httpcookieprocessor (Cookie)Ten #opener = Urllib2.build_opener (hander) One AUser_agent = {'user-agent':'mozilla/5.0 (Windows NT 10.0) applewebkit/537.36 (khtml, like Gecko) chrome/49.0.2623.110 safari/537.36'} -Cookie = {'_t_wm=622b9aac959576e1ec052536bf788ca6; Sub=_2a256nuabderxgevi41ey8czmwj-ihxvz2irtrdv6pujbstbelwvckw1lhesb2ltzebke-ktx8edhhrigsk-45q.; subp=0033wrsxqpxfm725ws9jqgmf55529p9d9w5e1dxuxiriqkqslke2rx2s5jpx5o2p5nhd95q0son01k5eeh.0; Suhb=0phbsdb4ewaogh; ssologinstate=1462933195; GSID_CTANDWM=4UBECPOZ59AQGOFSYRXSVFSLM1V'} - theHeader = { - 'user-agent': User_agent, - 'Cookies': Cookies - } +URL ='http://weibo.cn/u/5305630013' -Request = Urllib2. Request (url,headers=header) +Response =Urllib2.urlopen (Request) ASoup = BeautifulSoup (Response,'lxml', from_encoding='Utf-8') at Printsoup.prettify () - -Titles = Soup.find_all ('span', class_='CTT') - forTitleinchtitles: - PrintTitle.get_text ()
Originally wanted to use the Cookielib module to obtain the cookie, but only to obtain the first semicolon content, directly find in the F12 inside find a cookie, try the next effect, found to be a cookie problem directly first write the code!
Places to be aware of:
1, the first is the cookie, it is best to automatically get, I see I can finish this task today;
2, then is user-agent this, sometimes to have can, just in case, are added on it;
3, is that the headers, to be transmitted to the URL of the data should be placed in the headers inside, a start do not know directly to the cookie and user-agent is put in the request inside, error.
The last is to write to their own, slowly accustomed to object-oriented programming
Crawl Sina Weibo content (mobile Sina Weibo, PC version of the crawl content, PS: Can not find the URL!) )