亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python URL 編碼問題

Python URL 編碼問題

紅糖糍粑 2023-08-08 16:15:22
我正在嘗試修改 ANKI 插件 .py 文件,以便能夠解析牛津的單詞from urllib.parse import urlencodeqs = urlencode({"q": "come along"})URL = "https://www.oxfordlearnersdictionaries.com/search/english/?{}"print(URL.format(qs))我的輸出“https://www.oxfordlearnersdictionaries.com/search/english/?q=come+along”但實際上輸出應該是這樣的“https://www.oxfordlearnersdictionaries.com/definition/english/come-along?q=come+along”我怎樣才能克服這個問題我是初學者#-- coding:utf-8 --import randomfrom ..base import *import requestsfrom bs4 import BeautifulSoupfrom time import sleepfrom random import randint@register(u'Oxford_Article')class Oxford_Article(WebService):    def init(self):        super(Oxford_Article, self).init()    def _get_from_api(self):        sleep(randint(1,3))                param = {"q": "come along"}        qs = urlencode(param)        param['q'] = param['q'].replace(' ', '-')        URL = "https://www.oxfordlearnersdictionaries.com/definition/english/{}?{}"                data = self.get_response(URL.format(param['q'],qs))        soup = parse_html(data)        result = {            'Article': u'',        }        # Article        element = soup.find('div', id='entryContent')        for s in element.select('script'):            s.extract()        if element:            result['Article'] = u''.join(str(e) for e in element.contents)        return self.cache_this(result)    @export([u'entryContent', u'Article definition'])    def fld_definate(self):        return self._get_field('Article')
查看完整描述

2 回答

?
當年話下

TA貢獻1890條經驗 獲得超9個贊

你可以這樣做


from urllib.parse import urlencode

param = {"q": "come along"}

qs = urlencode(param)

param['q'] = param['q'].replace(' ', '-')

URL = "https://www.oxfordlearnersdictionaries.com/definition/english/{}?{}"

print(URL.format(param['q'],qs))


查看完整回答
反對 回復 2023-08-08
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

如果您在解析數據時遇到錯誤

只需替換這段代碼

data = self.get_response(URL.format(param['q'],qs))
soup = parse_html(data)

用這個代碼

page = urllib.request.urlopen(URL.format(param['q'],qs))
soup = BeautifulSoup(page)

也不要忘記導入這個

import urllib.request


查看完整回答
反對 回復 2023-08-08
  • 2 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號