新浪新聞按keyword抓取例項

2021-09-07 12:14:08 字數 1495 閱讀 5366

import urllib2

import requests

#import mysqldb

import webbrowser

import string

import re

from beautifulsoup import beautifulsoup

def gethtml(page):#獲取**內容

page=str(page)

html=requests.get(""+page).text

return html

def getpage():#獲得網頁總數

html=requests.get("

range=all&c=news&q=%bd%f0%d0%e3%cf%cd&from=home").text   #**

soup=beautifulsoup(''.join(html))

a=soup('div',)

race=

c=""

race=str(a).split("新聞")[1].split("篇")[0].split(",")   #獲取**有多少頁碼

b=len(race)

for i in range(b):

c+=race[i]

b=string.atoi(c)/20

return b

def getcontents(html):#獲取指定新聞內容

soup=beautifulsoup(''.join(html))

rs=re.compile("fgray_time")

html=soup.findall('span',attrs=)

rs=re.compile("box-result clearfix")

contents=soup.findall('div',attrs=)

for c in html:

length=len(c.text.split(' '))

if length==3:

source=c.text.split(' ')[0]#新聞**

time=c.text.split(' ')[1]+' '+c.text.split(' ')[2]#新聞發表時間

print source

print time

else:

time=c.text#新聞發表時間

source=''#新聞**

print time

for i in contents:

title= i.h2.a.text#新聞標題

content= i.p.text#新聞簡單介紹內容

##    print html

if __name__=="__main__":

count=getpage()

print 111

for i in range(count):

print getcontents(gethtml(i))

print 222

新浪新聞按關鍵字抓取例項

import urllib2 import requests import mysqldb import webbrowser import string import re from beautifulsoup import beautifulsoup def gethtml page 獲取 內容...

新浪新聞小偷

新浪新聞小偷 1.新浪 新聞抓取程式 host 127.0.0.1 mysql 主機名 namesql mysql 使用者名稱 passsql mysql 密碼 lib news 資料庫名 table news 資料庫表名 filename 抓取的新聞頁 key distop 新聞開始 新聞開始關鍵...

新浪的新聞發布規範

1.標題規範 2 所有標題通常必須句型完整,主謂賓齊全。標題應突出文章要點或最吸引人的內容,避免出現同樣詞語。3 所有標題必須明確表達文章內容,不得給人以模稜兩可之感。仔細判斷文章含義,避免因題目理解問題造成法律糾紛和政治錯誤。4 標題用詞盡量通俗易懂,重要位置的標題不要出現過於專業或晦澀的詞語。5...