時間:2023-02-13 14:06:01 | 來源:建站知識
時間:2023-02-13 14:06:01 來源:建站知識
web爬蟲本質(zhì)上就是一種遞歸,首先獲取一個url對應的頁面內(nèi)容,檢測這個頁面尋找另一個url,再獲取該url對應的網(wǎng)頁內(nèi)容,并不斷循環(huán)這一過程。這也就是代碼在運行的時候是那么慢的原因了...import reimport sslimport urllib.requestfrom bs4 import BeautifulSoup
def visit_page(url): """ 解析主url的函數(shù),并返回BeautifulSoup解析后的html :param url: :return: """ headers = {"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36" } ssl._create_default_https_context = ssl._create_unverified_context url_obj = urllib.request.Request(url, headers=headers) response = urllib.request.urlopen(url_obj) html = response.read().decode('utf8') return BeautifulSoup(html, "html.parser")
def get_url_links(): """ 利用正在獲取內(nèi)鏈,獲取標簽為href的,然后塞到集合中去重并返回集合 :return: """ links_url = set() bs = visit_page("https://movie.douban.com/chart") data = bs.find_all('a', href=re.compile('(https:////movie/.douban/.com//subject).*')) count = 0 for i in data: if 'href' in i.attrs: links_url.add(i.attrs['href']) count += 1 print(count) return links_url
def visit_movie(): """ 循環(huán)集合數(shù)據(jù),并通過正則找到標簽為h1的數(shù)據(jù),獲取文本信息 文本通過strip去兩邊的換行符并添加到列表中,返回列表 :return: """ save_data = [] for i in get_url_links(): bs = visit_page(i) title = bs.find('h1').get_text() save_data.append((title.strip('/n'),)) return save_data
if __name__ == '__main__': """ 循環(huán)列表,通過換行符,分割電影名稱和年份 獲取到電影名稱和年份,end不換行顯示,print()換行第二行數(shù)據(jù) """ for i in visit_movie(): for j in i: name, year = j.split('/n') print(f'電影名稱: {name} ;年份: {year}', end=' ') print()
電影名稱: 第十一回 ;年份: (2019) 電影名稱: 上帝之城 Cidade de Deus ;年份: (2002) 電影名稱: 托芙 Tove ;年份: (2020) 電影名稱: 三傻大鬧寶萊塢 3 Idiots ;年份: (2009) 電影名稱: 我不是藥神 ;年份: (2018) 電影名稱: 未麻的部屋 Perfect Blue ;年份: (1997) 電影名稱: 電鋸驚魂9:漩渦 Spiral: From the Book of Saw ;年份: (2021) 電影名稱: 罪之聲 罪の聲 ;年份: (2020) 電影名稱: 那些希望我死的人 Those Who Wish Me Dead ;年份: (2021) 電影名稱: 人之怒 Wrath of Man ;年份: (2021) 電影名稱: 尋龍傳說 Raya and The Last Dragon ;年份: (2021) 電影名稱: 花樣年華 花樣年華 ;年份: (2000) 電影名稱: 找到你 Finding You ;年份: (2021) 電影名稱: 地球改變之年 The Year Earth Changed ;年份: (2021) 電影名稱: 蝙蝠俠:黑暗騎士崛起 The Dark Knight Rises ;年份: (2012) 電影名稱: 穿越時空的少女 時をかける少女 ;年份: (2006) 電影名稱: 小人物 Nobody ;年份: (2021) 電影名稱: 漸強 Crescendo ;年份: (2019) 電影名稱: 釜山行 ??? ;年份: (2016) 電影名稱: 迷宮中的人 L'uomo del labirinto ;年份: (2019) 電影名稱: 遺愿清單 The Bucket List ;年份: (2007) 電影名稱: 智能大反攻 The Mitchells vs. The Machines ;年份: (2021) 電影名稱: 八月處子 La Virgen de Agosto ;年份: (2019) 電影名稱: 真人快打 Mortal Kombat ;年份: (2021) 電影名稱: 窗里的女人 The Woman in the Window ;年份: (2021) 電影名稱: 茲山魚譜 ???? ;年份: (2021) 電影名稱: 黑道與家族 ヤクザと家族 ;年份: (2021) 電影名稱: 網(wǎng)誘驚魂 Profile ;年份: (2018) 電影名稱: 夏季幸存者 I?gyventi vasar? ;年份: (2018) 電影名稱: 三塊廣告牌 Three Billboards Outside Ebbing, Missouri ;年份: (2017) 電影名稱: 酒精計劃 Druk ;年份: (2020) 電影名稱: 教父2 The Godfather: Part Ⅱ ;年份: (1974) 電影名稱: 今天在這里 Here Today ;年份: (2021) 電影名稱: 人潮洶涌 ;年份: (2021) 電影名稱: 鬼滅之刃 Demon Slayer ;年份: (2003) 電影名稱: 哥斯拉大戰(zhàn)金剛 Godzilla vs Kong ;年份: (2021) 電影名稱: 鬼滅之刃 劇場版 無限列車篇 劇場版 鬼滅の刃 無限列車編 ;年份: (2020) 電影名稱: 唐人街探案3 ;年份: (2021) 電影名稱: 初戀這件小事 ???????????????????????...??? ;年份: (2010) Process finished with exit code 0
關(guān)鍵詞:電影,獲取,名稱,爬蟲,網(wǎng)絡
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。