043 Python语法之网络请求urllib

urllib模块

方法

urllib.request.urlopen(网址)

  1. 打开一个网址,返回一个请求对象(request)

request.read()

  1. 返回值是二进制的

request.readline()

  1. 返回值是二进制的
  2. 需要用 decode(“utf-8”)进行解码

抓取天涯邮箱,逐行读取

import re
import urllib
import urllib.request

mailRegex = re.compile("([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})", re.IGNORECASE)
mailList = []
for line in urllib.request.urlopen("http://bbs.tianya.cn/m/post-140-393974-6.shtml"):
    myList = mailRegex.findall(line.decode("utf-8"))
    if myList:
        mailList.extend(myList)

print(mailList)

抓取天涯邮箱,全部读取

import re
import urllib
import urllib.request

mailRegex = re.compile("([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})", re.IGNORECASE)
mailList = []
myStr = urllib.request.urlopen("http://bbs.tianya.cn/m/post-140-393974-6.shtml").read()
mailList.extend(mailRegex.findall(myStr.decode("utf-8")))
print(mailList)

股票代码以及股票所在公司名抓取

下载

tup1 = urllib.request.urlretrieve("网址","1.jpg")

urlretrieve()

  1. 参数1:网址
  2. 参数2:目标地址
  3. 返回值一个元组(“存储地址”, “HttpMessage实例”)

股票数据下载

url="http://quotes.money.163.com/service/chddata.html?code=1300133&end=20130523&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP"

链接分析

  1. code=1300133,1代表深市,0代表沪市,后面接着的是股票代码
  2. end=20130523,股票交易日期

下载python库

xml支持

pip install lxml    # lxml支持

处理数据

pip install pandas  # 处理数据

科学计算

pip install numpy   # 科学计算

财经数据接口

pip install tushare # 财经数据接口
发布了151 篇原创文章 · 获赞 26 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/runnoob_1115/article/details/102952415