小编Pot*_*Box的帖子

在 tkinter 中显示来自 url 的图像

正如标题所说,我想从 url 显示图像,而无需下载。到目前为止我的代码:

from bs4 import BeautifulSoup as soup
from tkinter import *
import urllib.parse
from PIL import Image, ImageTk
import io

website = "http://www.porcys.com/review/"
openWebsite = soup(urllib.request.urlopen(website), 'html.parser')

reviews = openWebsite.find(name="section", attrs={'class': 'slider-content review'}).ul

results = []
for a in reviews(href=True):
    temp = "http://www.porcys.com"+a['href']
    results.append(temp)

mainWindow = Tk()
mainWindow.title("Latest Porcys reviews")

for i in range(0, 8):
    review = results[i]
    openReview = soup(urllib.request.urlopen(review), 'html.parser')
    rating = openReview.find(name="span", attrs={'class': 'rating'})
    album = openReview.find(name="div", attrs={'class': 'wrapper'}).i
    artist = openReview.find(name="div", attrs={'class': 'wrapper'}).h2
    coverWIP …
Run Code Online (Sandbox Code Playgroud)

python tkinter python-3.5

3
推荐指数
1
解决办法
9745
查看次数

Django Rest Framework:将用户从视图传递到序列化器

我有这样的用户更改密码视图:

class ChangePasswordView(generics.UpdateAPIView):
    serializer_class = ChangePasswordSerializer
    permission_classes = [IsAuthenticated]

        def put(self, request, *args, **kwargs):
            data = request.data.copy()
            data['user'] = self.request.user
            serializer = self.get_serializer(data=data)
            serializer.is_valid(raise_exception=True)
            user = serializer.validated_data['user']
            user.set_password(serializer.validated_data["new_password"])
            user.save()
            return Response(status=status.HTTP_204_NO_CONTENT)
Run Code Online (Sandbox Code Playgroud)

此视图的序列化器如下所示:

class ChangePasswordSerializer(serializers.Serializer):
    old_password = serializers.CharField()
    new_password = serializers.CharField()
    new_password_retyped = serializers.CharField()

    def validate(self, data):
        old_password = data.get('old_password')
        new_password = data.get('new_password')
        new_password_retyped = data.get('new_password_retyped')
        user = data.get('user')

        # misc validation checks

        data['user'] = user
        return data
Run Code Online (Sandbox Code Playgroud)

我的问题是用户对象没有传递给序列化器,尝试打印它以查看其中的数据内容put

<QueryDict: {'old_password': ['testpassword'], 'new_password': ['testpassword1'], 'new_password_retyped': ['testpassword1'], 'user': [<User: root>]}> …
Run Code Online (Sandbox Code Playgroud)

django django-rest-framework

2
推荐指数
1
解决办法
261
查看次数

LINQ To XML错误:select子句中表达式的类型不正确."Select"调用中的类型推断失败

我试图读取XML文件,但type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'由于以下查询而收到错误:

List<Data> dogs = (from q in doc.Descendants("dog")
    where (string)q.Attribute("name") == dogName
        select new Data
        {
            name = q.Attribute("name").Value,
            breed = q.Element("breed").Value,
            sex = q.Element("sex").Value
        }.ToList<Data>);
Run Code Online (Sandbox Code Playgroud)

数据类:

public class Data
{
    public string name { get; set; }
    public string breed { get; set; }
    public string sex { get; set; }
    public List<string> dogs { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

c# linq-to-xml visual-studio-2010

1
推荐指数
1
解决办法
1077
查看次数

Beautifulsoup保存链接到列表

我正在尝试从某个网站检索链接并将其保存到列表中以供进一步使用.到目前为止我得到的(这是我存储输出的.txt文件):

<ul>
<li><a class="active" href="/review/huerco-s-those-you-who-have-never-and-also-those-w/">Huerco S.<span><i>For Those Of You Who Have Never (And  Also Those Who Have)</i></span></a></li>
<li><a href="/review/dam-funk-dj-kicks/">Dâm-Funk<span><i>DJ-Kicks</i>  </span></a></li>
<li><a href="/review/skepta-konnichiwa/">Skepta<span><i>Konnichiwa</i></span></a></li>
<li><a href="/review/jessy-lanza-oh-no/">Jessy Lanza<span><i>Oh No</i></span></a></li>
<li><a href="/review/radiohead-moon-shaped-pool/">Radiohead<span><i>A Moon Shaped Pool</i></span></a></li>
<li><a href="/review/brodka-clashes/">Brodka<span><i>Clashes</i></span></a></li>
<li><a href="/review/james-blake-colour-anything/">James Blake<span><i> The Colour In Anything</i></span></a></li>
<li><a href="/review/kamaiyah-good-night-ghetto/">Kamaiyah<span><i>A Good Night In The Ghetto</i></span></a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我想将这些特定链接提取到列表中.我的代码:

website = "http://www.porcys.com/review/"
openWebsite = soup(urllib.request.urlopen(website), 'html.parser')
reviews = openWebsite.find(name="section", attrs={'class': 'slider-content review'}).ul
for a in reviews(href = True):
    temp = str("http://www.porcys.com"+str(a['href']))
    print(temp)
Run Code Online (Sandbox Code Playgroud)

打印输出正是我想要的(这意味着 - 只是链接,没有任何HTML标签),但我将它移动到列表有困难.我试过了

results[a] = temp
a+=1
Run Code Online (Sandbox Code Playgroud)

但我得到"列表索引必须是整数或切片,而不是标签"

python beautifulsoup

-1
推荐指数
1
解决办法
708
查看次数