V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
mercedes63
V2EX  ›  Python

新人求助 请各位老师指正 代码如下

  •  
  •   mercedes63 · 2017-04-09 08:48:46 +08:00 · 1684 次点击
    这是一个创建于 2580 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import urllib2,re
    f=open('./aa.txt','w')
    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
    response=urllib2.Request(url="http://www.foxnews.com/us.html",headers =headers)
    html=urllib2.urlopen(response).read()
    r=re.compile(r'<a href="/(?P<date>.+)">(?P<title>.+)</a></h3>')
    news=r.findall(html)
    for i in range(len(news)):
    date=news[0]
    title=news[1]
    f.write(title+' /'+date+'\n')
    f.close()
    目的是抓取这个网页的关键字并保存为文件 现在已实现保存一段新闻标题
    但是我想要的是现在的结果中的特定关键字 比如包含 news 这某一个单词的整段文字 初学不知怎么写代码了 求赐教
    2 条回复    2017-04-12 16:25:07 +08:00
    zsz
        1
    zsz  
       2017-04-09 11:03:57 +08:00 via iPhone   ❤️ 1
    用 in 判定单词是否在内容中,或者 find 判定下,还有用 beautiful soup 解析获取的页面更方便些

    有问题可以加我们的群问,这样效率更高,这个群是一群工程师组建的面向初学者的 python Linux 学习群( qq 群号: 278529278 ) ,非商业性质,拒绝广告,只接收真正想学这方面技术的朋友,交流学习,申请请说明来自 v2ex
    ryanzyy
        2
    ryanzyy  
       2017-04-12 16:25:07 +08:00
    https://scrapy.org/

    请抵制 重新发明轮子 的诱惑
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2244 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:41 · PVG 13:41 · LAX 22:41 · JFK 01:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.