Python網絡數據采集
阿新 • • 發佈:2017-09-23
html now() 數據采集 ont 網絡數據 函數 網絡 mytag dal
一、正則表達式
* 表匹配0次或者多次 a*b*
+ 表至少一次
[ ] 匹配任意一個
( ) 辨識一個編組
{m,n} m或者n 次
[^] 匹配任意不在中括號裏的字符
| 表示或者
. 表示匹配任意字符
^ 表字符的開始 ^a 表示以a開始
\ 表示轉義字符
$ 和^ 相反 從字符串的末尾開始匹配
?! 不包含
二、獲得屬性
獲得一個標簽的全部屬性
myTag.attrs
獲取圖片的資源位置src
myImgTag.attrs["src"]
獲取網頁的函數:
random.seed(datetime.datetime.now())
def getLinks(articleUrl):
html = urlopen("http://en.wikipedia.org"+articleUrl)
bs0bj = BeautifulSoup(html)
return bs0bj.find("div",{"id":"bodyContent"}).findAll("a",herf=re.compile("^(/wiki/)((?!:).)*$"))
links = getLinks("/wiki/Kevin_Bacon")
while len(links) > 0:
newArticle = links[random.randint(0,len(links)-1)].attrs["href"]
print(newArticle)
link = getLinks(newArticle)
Python網絡數據采集