2018-11-19

from urllib import request
import time
from lxml import etree

def autoBlog():
    url = 'https://www.jianshu.com/p/0b4c9e9931f5'
    headers = {
        "Referer": "https://www.jianshu.com/",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
    }
    req = request.Request(url=url,headers=headers)
    response = request.urlopen(req)
    content = response.read().decode()
    #复制html中的文本的XPath
    ##/html/body/div[1]/div[2]/div[1]/h1
    # print(content)
    xpath_content = etree.HTML(content)
    new_content = xpath_content.xpath('/html/body/div[1]/div[2]/div[1]/h1/text()')
    #print(new_content)
    with open('3.txt','w+',encoding='utf-8') as f:
        for i in new_content:
            f.writelines(i)
i=0
while i<100:
    autoBlog()
    i=i+1
    print(i)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 随着白天的慢慢变长,六月很快就到了,一年一度的高考就要来了。兰德中学每年都会被设为高考的考场,今年也不例外,为方便...
    蓝天航空幼儿园阅读 1,774评论 0 2
  • 今天是星期五,上午我开心地过完,下午我们要科学、品社都抽测(也就是期未考试) 时间就像在飞逝,转眼间到...
    李妙柯妈妈阅读 767评论 0 0
  • 出于本能,人的恐惧,最接于莫过来源于生命受到威胁。 要么,是自己的。 二战前的太平洋海面上,漂浮着一只救生艇,上面...
    三世织阅读 1,358评论 0 0