【问题标题】:Python replace url lib.requests with requests and BeautifulSoupPython 用 requests 和 BeautifulSoup 替换 url lib.requests
【发布时间】:2017-01-29 21:16:43
【问题描述】:

我使用 urllib 编写了以下函数,但我想将其更新为仅依赖于获取信息的请求,以及用于解析 xml 数据的 BeautifulSoup。这可能吗?

import re
import urllib.request
import urllib.parse
import xml.dom.minidom

def magnetic_variation(coordinates):
    rc = []
    lat, lon = coordinates
    params = urllib.parse.urlencode({'lat1': lat, 'lon1': lon, 'resultFormat': 'xml', 'startMonth': now.month})
    url = urllib.request.urlopen("http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?%s" % params) 
    dom = xml.dom.minidom.parseString(url.read())
    for node in dom.getElementsByTagName("declination")[0].childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    rawdata = ''.join(rc)    
    variation = str(re.findall(r"[-+]?\d*\.\d+|\d+", rawdata)[0])
    return round(float(variation),2)

【问题讨论】:

    标签: python web-scraping beautifulsoup python-requests urllib


    【解决方案1】:
    import requests, bs4
    
    def magnetic_variation(coordinates):
        rc = []
        lat, lon = coordinates
        params = {'lat1': lat, 'lon1': lon, 'resultFormat': 'xml', 'startMonth': now.month}
        url = requests.get("http://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination", params=params) 
        dom = bs4.BeautifulSoup(url.text)
    
    # parse the dom
    
    return round(float(variation),2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 1970-01-01
      • 2017-01-10
      相关资源
      最近更新 更多