1.网上有类似的服务吗?
给一个网址如www.baidu.com,返回网页的标题 百度一下
2.有能满足这个需求的源码吗?
因为什么样的网站都有自己写还要处理编码什么的。
给一个网址如www.baidu.com,返回网页的标题 百度一下
2.有能满足这个需求的源码吗?
因为什么样的网站都有自己写还要处理编码什么的。
|
nervounaApr 18, 2014
1
你确定你只要 title?这算是抓取网页里最简单的一件事了吧……
Google Beautiful Soup |
|
ericlsApr 18, 2014 via Android
2
手机打一个
from pyquery import PyQuery as pq d=py(url) d('title').text 不知道对不对 |
|
fayApr 18, 2014
3
我在开发自己的莲蓬网的时候,需要获取网页标题等信息,顺手抽离了这部分的代码: https://github.com/fay/pagemeta
|
|
tonghuashuaiApr 18, 2014
4
1 #!/usr/bin/env python
2 #coding:utf-8 3 4 import urllib 5 import re 6 7 8 def get_title(url): 9 title = '' 10 c = urllib.urlopen(url) 11 html = c.read() 12 13 p = '<title>.*?</title>' 14 target = re.findall(p, html) 15 16 if target: 17 title = target[0] 18 19 return title 20 21 if __name__ == '__main__': 22 url = 'http://www.baidu.com' 23 title = get_title(url) 24 print title 简单实现,没加异常处理 |
|
tonghuashuaiApr 18, 2014
5
行号杯具了
|
|
davidliApr 18, 2014
6
楼上正解
网站的标题应该都是在<title></title>里吧。 |
|
yhfApr 18, 2014 via iPhone
7
BeautifulSoup 随便哪个节点由你抓
|
|
lifemaxerApr 18, 2014
8
以当前页为例,
soup = BeautifulSoup(content) #content为当前页数据 a = soup.find_all('title')[0].get_text() |
|
9
|
|
10
@tonghuashuai
这个在实际应用中会出错的,获取的title编码不知道是什么,还要获取网页里面的charset,然后解码。 更恶心的是网页中写的charset又不一定是正确的(就遇到过这样的奇葩网站),然后用charset去解又会出错。 |
|
lm902Apr 19, 2014
12
text.split("<title>")[1].split("</title>")[0]
|
|
13
|
|
14
@kehr 使用bs4,用的就是你发到链接里面的例子。
soup.title.string # u'The Dormouse's story' 在用的过程中还是遇到乱码了,就是用这个方法获取这个网址的title是乱码,不知有解不? http://www.joy.cn/ |
|
cloverstdMay 25, 2014
15
乱码可能是 gzip 压缩了
|
|
caomuMay 25, 2014
17
1.网上有类似的服务吗?
> 关于这个我想到的是 YQL 。。。 |
|
SylvMay 25, 2014
18
@hao1032
import requests from bs4 import BeautifulSoup r = requests.get("http://www.joy.cn") r.encoding = requests.utils.get_encodings_from_content(r.content)[0] soup = BeautifulSoup(r.text) print soup.title.string 参考: http://liguangming.com/python-requests-ge-encoding-from-headers |
|
messagebusMay 25, 2014 via Android
19
@Sylv 18楼和我的方法一样,如果乱码最后一句改print soup.title.get_text()
|
|
dbowMay 25, 2014
20
上lxml用XPATH表达式最快,"html/head/title"
即 from lxml import etree etree.HTML(content).xpath("/html/head/title")[0] |
|
22
@Sylv 这个要加个import,我看了这个文章http://www.cnblogs.com/todoit/archive/2013/04/08/3008513.html
加了个from_encoding="gb18030",测试这个网站是可以的。 如果后面有问题,再使用你的这个方法。 |
|
xiaowanggeSep 5, 2014
23
#!/usr/bin/env python
# -*- coding: utf-8 -*- import requests from lxml import html response = requests.get('http://www.joy.cn/') # Parse the body into a tree parsed_body = html.fromstring(response.text) print ''.join(parsed_body.xpath('//title/text()')) |
|
funnybunny00Aug 30, 2017
24
福音
|