สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

Re: สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

โดย MBMoo » 15/06/2020 8:52 am

mindphp เขียน: 14/06/2020 2:21 am ปัญหา นี้เป็นยังไงบ้าง
เรียบร้อยแล้วค่ะ

Re: สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

โดย mindphp » 14/06/2020 2:21 am

ปัญหา นี้เป็นยังไงบ้าง

Re: สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

โดย jirawoot » 11/06/2020 5:20 pm

ลองใส่อันนี้ดูครับ

โค้ด: เลือกทั้งหมด

from urllib.request import urlopen, Request

req = Request(URL, headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'})
html = str(urlopen(req).read())

Re: สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

โดย MBMoo » 11/06/2020 4:19 pm

jirawoot เขียน: 11/06/2020 4:17 pm แปะโค้ดทั้งหมดมาหน่อยครับ

โค้ด: เลือกทั้งหมด

from flask import  Flask, render_template, request
import socket, time
from urllib.parse import urlparse
from urllib.request import urlopen
from bs4 import BeautifulSoup
import psycopg2 as p
import re

@app.route('/url' , methods=['POST', 'GET'])
def url():
    if request.method == 'POST':
        URL = request.form['url']
        print (URL, 'timing:')
        try:
            con = p.connect(host='127.0.0.1', database='test', user='postgres', password='0845770809')
            cur = con.cursor()
            urlinfo = urlparse(URL)

            start = time.time()
            ip = socket.gethostbyname(urlinfo.netloc)
            dns_tm = time.time() - start
            print('DNS:\t\t{:.3f} seconds'.format(dns_tm))

            start = time.time()
            html = str(urlopen(URL).read())
            load_tm = time.time() - start
            print('load:\t\t{:.3f} seconds'.format(load_tm))
            print('w/o DNS:\t{:.3f} seconds'.format(load_tm - dns_tm))

            sql = """INSERT INTO ur (url,dns,load_ur,wo_dns)
                        VALUES ('%s','%s','%s','%s') RETURNING id;""" % (URL,dns_tm,load_tm,load_tm - dns_tm)
            sql = sql.encode('utf-8')
            cur.execute(sql)
            id1 = str(cur.fetchone()[0])
            con.commit()
            soup = BeautifulSoup(html,"html.parser")

            for link in soup.find_all('a' ,href=re.compile('^https')):

                # time.sleep(1)
                print(link.get('href'))
                urlinfo2 = urlparse(link.get('href'))

                start = time.time()
                ip = socket.gethostbyname(urlinfo2.netloc)
                dns_tm2 = time.time() - start
                print('DNS:\t\t{:.3f} seconds'.format(dns_tm2))

                start = time.time()
                html = str(urlopen(link.get('href')).read())
                load_tm2 = time.time() - start
                print('load:\t\t{:.3f} seconds'.format(load_tm2))
                print('w/o DNS:\t{:.3f} seconds'.format(load_tm2 - dns_tm2))


                sql = """INSERT INTO des_ur (url,dns,load_ur,wo_dns,ur_id)
                                            VALUES ('%s','%s','%s','%s','%s')""" % (
                link.get('href'), dns_tm, load_tm, load_tm - dns_tm,id1)
                sql = sql.encode('utf-8')
                cur.execute(sql)
                con.commit()


            return html

        except p.DatabaseError:
            print('Error %s' % p.DatabaseError)

Re: สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

โดย jirawoot » 11/06/2020 4:17 pm

แปะโค้ดทั้งหมดมาหน่อยครับ

Re: สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

โดย MBMoo » 11/06/2020 3:15 pm

code ไม่สามารถใช้กับ บาง url ได้ค่ะมีวิธีแก้ยังไงคะ

โค้ด: เลือกทั้งหมด

html = urlopen(URL).read()
C++ & java & Python-1.png

Re: สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

โดย MBMoo » 11/06/2020 2:56 pm

แก้ไขด้วยการ เอา if ออก แล้ว for เปลี่ยนมาใช้ คำสั่งนี้แทน

โค้ด: เลือกทั้งหมด

for link in soup.find_all('a' ,href=re.compile('^https')):

สอบถามค่ะ บาง url ที่ดึงมา ใช้ไม่ได้ค่ะ

โดย MBMoo » 11/06/2020 2:39 pm

บาง url ที่นำมา for แล้ว if ต่อเลือกเฉพาะ a ที่เป็น https ใช้ไม่ได้ค่ะ ทั้งๆ ที่ก็เป็น https แต่กลับ error
code ไม่สามารถใช้กับ บาง url ได้ค่ะมีวิธีแก้ยังไงคะ

โค้ด: เลือกทั้งหมด

html = urlopen(link.get('href')).read()
C++ & java & Python-1.png
ปิดตัว if จะสามารใช้ได้หมด
C++ & java & Python-1.png
C++ & java & Python-1.png (146.69 KiB) Viewed 1246 times


แต่เมื่อเปิด if จะรันสุดถึงแค่ none ค่ะ
C++ & java & Python-2.png
C++ & java & Python-2.png (158.63 KiB) Viewed 1246 times

และบาง url ไม่สามารถใช้ ได้ค่ะทั้งๆที่เป็น https

โค้ด: เลือกทั้งหมด

html = urlopen(URL).read()

ข้างบน