小编Far*_*him的帖子

Scrapy:如何提取嵌套div(xpath选择器)中的内容?

请参阅下面的 html 标记。如何使用Scrapy中的xpath选择器从div中的col-sm-7类名中提取内容?

我想提取这段文字:

Infortrend EonNAS Pro 850X 8 盘位塔式 NAS,带 10GbE

HTML:

<div class="pricing panel panel-primary">
   <div class="panel-heading">Infortrend Products</div>
   <div class="body">
    <div class="panel-subheading"><strong>EonNAS Pro Models</strong></div>
    <div class="row">
     <div class="col-sm-7"><strong>Infortrend EonNAS Pro 850X 8-bay Tower NAS with 10GbE</strong><br />
      <small>Intel Core i3 Dual-Core 3.3GHz Processor, 8GB DDR3 RAM (Drives Not Included)</small></div>
     <div class="col-sm-3">#ENP8502MD-0030<br />
      <strong> Our Price: $2,873.00</strong></div>
     <div class="col-sm-2">
      <form action="/addcart.asp" method="get">
       <input type="hidden" name="item" value="ENP8502MD-0030 - Infortrend EonNAS Pro 850X 8-bay Tower NAS with 10GbE (Drives …
Run Code Online (Sandbox Code Playgroud)

python xpath web-crawler scrapy

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

具有反向关系的Django rest框架序列化器

我有两个employeeperson模型有关系但与模型person没有关系的employee模型。

喜欢:

class Person(models.Model):
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=100)

class Employee(models.Model):
    person = models.ForeignKey(Person, related_name='person_info')
    code = models.CharField()
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我需要code人员序列化程序中的字段数据。


我可以使用个人模型中的编写方法或在个人序列化器中使用SerializerMethodField来解决此问题

像这样:

def get_employee_code(self):
    return Employee.objects.get(person=self).id
Run Code Online (Sandbox Code Playgroud)

并将其添加为亲自序列化器中的源

employee_code = serializers.CharField(source='get_employee_code')
Run Code Online (Sandbox Code Playgroud)

或将员工序列化程序添加到人员序列化程序中

class PersonSerializer(serializers.ModelSerializer):
    employee = EmployeeSerializer()
    class Meta:
        model = Person
        fields = ('name', 'address', 'employee')
Run Code Online (Sandbox Code Playgroud)

但是我试图用反向关系做到这一点,但我做不到。我已经尝试过这样,它给出了一个错误

序列化器:

class PersonSerializer(serializers.ModelSerializer):
    employee_code = serializers.CharField(source='person_info.code')
    class Meta:
        model = Person
        fields = ('name', 'address', 'employee_code')
Run Code Online (Sandbox Code Playgroud)

我该如何解决反向关系呢?

python django django-models django-related-manager django-rest-framework

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