爬虫第一天

爬虫步骤:

1. 导入requests

import requests

2. url和请求头
url = 'http://example.webscraping.com/places/default/user/login'	#要请求的页面地址
headers = {'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0'}
3. 暂存数据
req = requests.get(url=url).content.decode()
#下面这个可以不要的
with open('example.html','w',encoding='utf-8') as f:
	f.write(req)
4. 数据过滤
req_key = re.findall(r'name="_formkey" type="hidden" value="(.*?)"',req)[0]
5. 数据保存
with open('date.txt’,'w',encoding='utf-8') as f:
	f.write(req_key)
补充:

能够使用with的条件:具有__enter__和__exit__方法

class WithObject(object):
    def __enter__(self):
        pass
    def __exit__(self, exc_type, exc_val, exc_tb):
        pass
翻译小程序:
import requests
import sys
import json
word = ' '.join(sys.argv[1:])

req_url = 'http://fanyi.youdao.com/translate'
Form_Date = {}
Form_Date['i'] = word
Form_Date['doctype'] = 'json'

response = requests.post(req_url,data=Form_Date)
html = response.content.decode()
html = json.loads(html)
print('❤ '*len(word))
print(word)
print('❤ '*len(word))
print(html['translateResult'][0][0]['tgt'])
print('❤ '*len(word))
结果:

运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值