快乐大本营失恋33天,鹤顶红,永泰千江月休闲农场
requests是一个很实用的python http客户端库,编写爬虫和测试服务器响应数据时经常会用到。可以说,requests 完全满足如今网络的需求。本文重点给大家介绍python使用requests模块实现爬取电影天堂最新电影信息,具体内容如下所示:
在抓取网络数据的时候,有时会用正则对结构化的数据进行提取,比如 href=""等。python的re模块的findall()函数会返回一个所有匹配到的内容的列表,在将数据存入数据库时,列表数据类型是不被允许的,而是需要将其转换为元组形式。下面看下,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()函数来实现 # 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()函数来实现。join()函数是这样描述的:
""" s.join(iterable) -> str return a string which is the concatenation of the strings in the iterable. the separator between elements is s. """
join()函数使用时,传入一个可迭代对象,返回一个可迭代的字符串,该字符串元素之间的分隔符是“s”。
传入一个可迭代对象,可以使list,tuple,也可以是str。
s = 'asdf1234' sss = '@'.join(s) print(type(sss), sss)
总结
以上所述是小编给大家介绍的python使用requests模块实现爬取电影天堂最新电影信息,希望对大家有所帮助
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论