普奥云-Python爬虫工程师

1. Python语言中map函数返回类型是什么?

  • 在Python2中,map函数返回列表。
  • 在Python3中,map函数返回迭代器。

2. 请给出以下代码运行结果

import re

pattern = re.compile(r"(\w+)\s(\w+)")
s = "hello 123,hello 456,byebye789"
m = pattern.sub(r"hello world", s)
print(m)

输出结果:

hello world,hello world,byebye789

3. 请用BeautifulSoup或者Xpath任一方式找到以下id标签里面的内容?

<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>

答案:

html.xpath("//a[@id='link1']")

4. 爬虫一般获取http请求返回内容编码类型的途径有哪些?

在使用requests时,返回的response内容,我们可以使用response.rncoding来查看返回内容的编码类型

5. 简单说明requests.contentrequests.text的区别

requests.text返回的是Unicode类型的数据,如果想获取文本,可以使用request.text
requests.content返回的是bytes类型的数据,也就是二进制类型的数据,如果想获取图片,文件,则可以使用requests.content

6. 请用__new__方法来实现单例模式

class Singleton(object):
    def __new__(cls, *args, **kw):
        if not hasattr(cls, '_instance'):
            orig = super(Singleton, cls)
            cls._instance = orig.__new__(cls, *args, **kw)
        return cls._instance

class MyClass(Singleton):
    a = 1

7. "insert into business_info(create_time,date,from,websites) values (?,?,?,?)" 能否正常执行?如果不能,为什么?

可以

8. 简述scrapy或者scrapy-redis的抓取流程?

  • scrapy流程:
    • 引擎从调度器中取出一个链接(URL)用于接下来的抓取
    • 引擎把URL封装成一个请求(Request)传给下载器
    • 下载器把资源下载下来,并封装成应答包(Response)
    • 爬虫解析Response
    • 解析出实体(Item),则交给实体管道进行进一步的处理
    • 解析出的是链接(URL),则把URL交给调度器等待抓取

猜你喜欢

转载自blog.csdn.net/pythoncodez/article/details/80494528
今日推荐