使用urllib / BeautifulSoup4获取和读取URL时出错

时间:2017-10-07 19:34:14

标签: python

我目前正在学习如何使用BeautifulSoup并在网站上练习从中提取网址。我尝试运行时收到此错误:

from .forms import MatchForm
from home.models import Match
from soccer.models import Game, Player, Settings

@login_required
def create_match(request):
    form = MatchForm(request.POST or None)
    context = {
        'form': form,
    }
    if request.method == 'POST':
        if form.is_valid():
            if request.POST['pick'] == 'soccer':
                m = form.save()
                p = Player.objects.create(name=request.user)
                s = Settings.objects.create(match=m)
                g = Game.objects.create(match=m, player=p)
                return redirect('soccer:soccer')
    return render(request, 'create_match.html', context)

这是我的代码:

    Traceback (most recent call last):
  File "/home/yxty/Realex.py", line 7, in <module>
    uClient = uReq(my_url)
  File "/usr/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 472, in open
    response = meth(req, response)
  File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.5/urllib/request.py", line 510, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 405: Not Allowed

1 个答案:

答案 0 :(得分:0)

与推荐的Andrew Z一样,尝试使用class CustomerInfo(models.Model): customer_name=models.CharField('Customer Name', max_length=50) customer_mobile_no = models.CharField('Mobile No', null=True, blank=True,max_length=12) customer_price=models.IntegerField('Customer Price') customer_product_warrenty = models.CharField('Product Warrenty',null=True, blank=True,max_length=10) customer_sell_date = models.DateTimeField('date-published') customer_product_id=models.CharField('Product ID',max_length=150,null=True, blank=True) customer_product_name=models.CharField('Product Name', max_length=50) def __str__(self): return self.customer_name

def dailyReport(request):

    if request.method =='POST':
        datelist=request.POST['datewisesearch']

        try:
            customers = CustomerInfo.objects.filter(
                customer_sell_date__day=datelist)

            months=timezone.now().month
            years = timezone.now().year

            return render(request, 'shop/report.html', {"customers": customers, "datelist": datelist, "months": months, "years": years})


        except:
            pass

        return render(request,'shop/report.html', context=None)

您现在可以解析 {% extends "shop/base.html" %} {% block content_area %} <div class="col-lg-12"> <div class="daily_title"> <h2>Daily Report</h2> </div> <form action="{% url 'shop:dailyReport' %}" class="" method="POST"> {% csrf_token %} <div class="form-group"> <label for="datewisesearch">Search Product:</label> <input type="text" class="form-control" id="datewisesearch" name="datewisesearch"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> <div class="col-lg-12"> <br> <hr> <button type="button" class="btn btn-success">See Today Date is: {{datelist}}-{{months}}-{{years}} : Reports</button> <br> <hr> <div class="table table-bordered"> <table class="table"> <thead> <tr> <th>No</th> <th>Invoice ID</th> <th>Name</th> <th>Mobile</th> <th>Product Name</th> <th>Product ID</th> <th>Warrenty</th> <th>Customer Price</th> </tr> </thead> {% for x in customers %} <tbody> <tr> <td>{{ forloop.counter }}</td> <td>{{x.id}}</td> <td><a href="{% url "shop:customersProfile" x.id %}">{{x.customer_name}}</a></td> <td>{{x.customer_mobile_no}}</td> <td>{{x.customer_product_name}}</td> <td>{{x.customer_product_id}}</td> <td>{{x.customer_product_warrenty}} Month</td> <td>{{x.customer_price}} TK</td> </tr> </tbody> {% endfor %} <h2>Total Cost is:{{sum_price}} </h2> </table> </div> </div> {% endblock content_area %}