python使用requests模塊實現(xiàn)爬取電影天堂最新電影信息
requests是一個很實用的Python HTTP客戶端庫,編寫爬蟲和測試服務器響應數(shù)據(jù)時經(jīng)常會用到。可以說,Requests 完全滿足如今網(wǎng)絡的需求。本文重點給大家介紹python使用requests模塊實現(xiàn)爬取電影天堂最新電影信息,具體內(nèi)容如下所示:
在抓取網(wǎng)絡數(shù)據(jù)的時候,有時會用正則對結構化的數(shù)據(jù)進行提取,比如 href="https://www.1234.com"等。python的re模塊的findall()函數(shù)會返回一個所有匹配到的內(nèi)容的列表,在將數(shù)據(jù)存入數(shù)據(jù)庫時,列表數(shù)據(jù)類型是不被允許的,而是需要將其轉換為元組形式。下面看下,str/list/tuple三者之間怎么相互轉換。
class forDatas: def __init__(self): pass def str_list_tuple(self): s = 'abcde12345' print('s:', s, type(s)) # str to list l = list(s) print('l:', l, type(l)) # str to tuple t = tuple(s) print('t:', t, type(t)) # str轉化為list/tuple,直接進行轉換即可 # 由list/tuple轉換為str,則需要借助join()函數(shù)來實現(xiàn) # list to str s1 = ''.join(l) print('s1:', s1, type(s1)) # tuple to str s2 = ''.join(t) print('s2:', s2, type(s2))
str轉化為list/tuple,直接進行轉換即可。而由list/tuple轉換為str,則需要借助join()函數(shù)來實現(xiàn)。join()函數(shù)是這樣描述的:
""" S.join(iterable) -> str Return a string which is the concatenation of the strings in the iterable. The separator between elements is S. """
join()函數(shù)使用時,傳入一個可迭代對象,返回一個可迭代的字符串,該字符串元素之間的分隔符是“S”。
傳入一個可迭代對象,可以使list,tuple,也可以是str。
s = 'asdf1234' sss = '@'.join(s) print(type(sss), sss)
總結
以上所述是小編給大家介紹的python使用requests模塊實現(xiàn)爬取電影天堂最新電影信息,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!