Python遇到过得text和text()

 

1、BeautifulSoup中的.text方法和get_text()方法的区别

如果你想要用text值,应该调用对应的get_text(),

'cate': list(cate.stripped_strings)     obj.stripped_strings:高级的get_text()  ,外面加list可以列表化

soup.title
# <title>The Dormouse's story</title>
soup.title.string
# u'The Dormouse's story'

提高Beautiful Soup效率的办法,使用lxml作为解析器.Beautiful Soup用lxml做解析器比用html5lib(补全)或Python内置解析器速度快很多.

2、Scrapy爬虫

response.selector.xpath('//title/text()')     #xpath提取就是文本了,熟悉就@
提取文本数据,必须调用选择器.extract() 方法

只想提取第一个匹配的元素,可以调用选择器 .extract_first()

3、python requests的content和text方法的区别

1、resp.text返回的是Unicode型的数据。

2、resp.content返回的是bytes型也就是二进制的数据。

也就是说,如果你想取文本,可以通过r.text。

如果想取图片,文件,则可以通过r.content。(resp.json()返回的是json格式数据)

4、Python3  encode()方法

str1=str.encode(encoding='UTF-8',errors='strict')

str2 = str.encode("GBK") 

猜你喜欢

转载自blog.csdn.net/sinat_23880167/article/details/82868027